Database query method and device, electronic equipment and storage medium
By receiving natural language query requests and generating structured query instructions, the system automatically identifies the target database, solving the problem of low efficiency caused by the complexity of database queries and achieving intelligent and efficient data access.
Patent Information
- Application Number
- CN202511052007.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-29
- Publication Date
- 2025-11-11
AI Technical Summary
In existing technologies, database query methods are complex, resulting in low data query efficiency and high difficulty for non-technical users.
By receiving user query requests based on natural language input and combining them with session context data to generate structured query instructions, the system can automatically identify target databases and perform queries, thus simplifying the query process.
It enables intelligent database queries, lowers the data access threshold for non-technical users, improves query efficiency and accuracy, and simplifies the operation process.
Smart Images

Figure CN120929479A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data query technology, and in particular to a database query method, apparatus, electronic device and storage medium. Background Technology
[0002] With the advent of the big data era, enterprise data is experiencing explosive growth, distributed across different database systems. Traditional data query methods mainly rely on: SQL query tools such as Navicat and DataGrip, which require users to manually write SQL statements, demanding high technical skills; and BI tools such as Tableau and Power BI, which, while providing visual interfaces, are complex to configure and struggle to handle complex business logic. Summary of the Invention
[0003] In view of this, embodiments of this application provide a database query method, apparatus, electronic device, and storage medium to solve the problem that the database query method is complex and results in low data query efficiency in the prior art.
[0004] A first aspect of this application provides a database query method, the method comprising: receiving a query request from a user based on natural language input, and determining a target database from a database set based on the query request; obtaining session context data corresponding to the query request, and generating a structured query instruction for querying the target database based on the session context data and the query request; and querying the target database according to the structured query instruction to obtain query results.
[0005] A second aspect of this application provides a database query apparatus, comprising: a determining module, configured to receive a query request from a user based on natural language input, and determine a target database from a database set based on the query request; a generating module, configured to obtain session context data corresponding to the query request, and generate a structured query instruction for querying the target database based on the session context data and the query request; and a querying module, configured to query the target database according to the structured query instruction and obtain query results.
[0006] A third aspect of this application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the above-described method.
[0007] A fourth aspect of this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described method.
[0008] The beneficial effects of this application embodiment compared with the prior art are as follows: The method in this application embodiment receives a user's query request based on natural language input, and determines the target database from the database set based on the query request; obtains the session context data corresponding to the query request, and generates a structured query instruction for querying the target database based on the session context data and the query request; queries the target database according to the query instruction to obtain the query result. This method can accurately identify the query intent and automatically construct a structured query instruction by combining the user's natural language query request with the current session context data, thereby realizing intelligent database query, simplifying the database query method, improving the database query efficiency, and overcoming the technical problem that existing natural language query servers have difficulty understanding context and handling complex query logic. This significantly reduces the data access threshold for non-technical users, improves the accuracy and automation of the query, and avoids the problem of low data query efficiency caused by complex database query methods in the prior art. Attached Figure Description
[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 This is a schematic flowchart of a database query method provided in an embodiment of this application;
[0011] Figure 2 This is a schematic diagram of the structure of a database query device provided in an embodiment of this application;
[0012] Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0013] In the following description, specific details such as particular device structures and techniques are set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known devices, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this application with unnecessary detail.
[0014] A database query method and apparatus according to embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0015] Figure 1 This application provides a database query method, such as... Figure 1 As shown, the method includes:
[0016] S101. Receive a query request from a user based on natural language input, and determine the target database from the database set based on the query request;
[0017] S102. Obtain the session context data corresponding to the query request, and generate a structured query instruction for querying the target database based on the session context data and the query request.
[0018] S103. Query the target database according to the structured query command to obtain the query results.
[0019] It is understood that the database query provided in this application can be applied to the terminal and / or the server. That is, the above steps can be performed by the server or the terminal alone, or by a combination of the server and the terminal. For better explanation, the following will take the execution by the server alone as an example.
[0020] This embodiment provides a database query method aimed at solving the problem of low data query efficiency caused by the complexity of existing database query methods. This method allows users to initiate query requests in natural language, and the server generates structured query instructions based on the query context, automatically identifying the target database for querying, thus improving the intelligence and operational efficiency of cross-database queries.
[0021] The server first receives a query request from the user terminal, which is input in natural language, such as "view the sales growth over the past month" or "which of the previously mentioned customer contracts are still pending." This query request can be entered as text or converted by a speech recognition module.
[0022] In some examples, the target database is determined from a database set based on the query request. This includes determining the target database corresponding to the query request based on at least one of the keywords, entity names, and query intent contained in the query request. For example, the server automatically filters semantically related target databases from a pre-defined database set based on keywords, entity names (such as "sales" or "customer contracts"), and query intent in the query request. For instance, a "sales growth" request might map to the "sales data table database," while a "customer contract" request might map to the "customer management database."
[0023] In some examples, the target database is determined from the database set based on the query request, including: identifying the data table corresponding to the query request, and then identifying the corresponding target database based on the data table. For example, if the user enters a query request of "query the data in the order table", all registered databases (database set) are scanned first, then the table name "order table" mentioned in the query request is identified, and a database containing this table is searched among all registered databases, returning the matching target database.
[0024] In some embodiments, the database collection may include multiple heterogeneous databases, such as MySQL, MongoDB, PostgreSQL, etc. The server maintains a database index mapping table or a topic-database association table to support semantic-level matching and database selection.
[0025] In some examples, to accurately understand user intent, the server further extracts session context data for the current session. This session context data includes, but is not limited to: natural language requests submitted by the user in the current session; the page or business process identifier of the current operation; and entity information (such as customer name, time range, etc.) from the previous query result.
[0026] By introducing contextual information, the server can eliminate ambiguity in user statements. For example, if a user enters "inquire about Zhang San's contract status" and "what extensions did he have last month?" consecutively, the server can correctly identify that "he" refers to "Zhang San" and retain the entity information from the previous input.
[0027] The server generates structured query instructions based on natural language query requests and corresponding context data. This step can be based on deep learning models, intent recognition algorithms, or template matching mechanisms to achieve automatic conversion from natural language to structured query languages (such as SQL, GraphQL, etc.).
[0028] For example, the query request "Zhang San's contract completion status last month" is parsed into the following SQL statement after considering the context:
[0029] {SELECT contract_id,status FROM contract_table
[0030] WHERE cl ient_name = 'Zhang San'
[0031] AND month = '2025-06'
[0032] AND status! = 'Completed'}.
[0033] The server applies the structured query commands to the selected target database and presents the query results to the user in the form of tables, graphs, reports, etc., or provides further filtering, export and other functions.
[0034] According to the solution provided in this application embodiment, a user's query request based on natural language input is received, and a target database is determined from a database set based on the query request; the session context data corresponding to the query request is obtained, and a structured query instruction for querying the target database is generated based on the session context data and the query request; the target database is queried according to the structured query instruction to obtain the query result. This method accurately identifies the query intent and automatically constructs a structured query instruction by combining the user's natural language query request with the current session context data, realizing intelligent database querying, simplifying the database query method, improving the database query efficiency, and overcoming the technical problem that existing natural language query servers have difficulty understanding context and handling complex query logic. This significantly reduces the data access threshold for non-technical users, improves the accuracy and automation of queries, and avoids the problem of low data query efficiency caused by complex database query methods in the prior art.
[0035] In some examples, based on session context data and query requests, structured query instructions for querying the target database are generated, including: parsing key entities and query intents in the query request; supplementing the key entities and query intents based on session context data; determining the target query template based on the query intent; and generating structured query instructions corresponding to the target database based on the key entities and the target query template.
[0036] The server performs semantic analysis on the query request, extracting key entities and query intent. Key entities refer to specific objects or condition values explicitly mentioned in the user's natural language query that can be used as database query parameters. They typically correspond to field values in the database and are used as WHERE conditions or query scope.
[0037] For example, the key entities obtained by parsing the query statement are shown in Table 1 below:
[0038] Table 1: Example table of key entities obtained by query statements
[0039] query statement Key Entities Inquire about Zhang San's contract information. Zhang San (customer name) Statistics on sales in the second quarter of 2024 Second quarter of 2024 (time range) Unfulfilled orders from last month Last month (time), incomplete (status) Payment status of customers in Beijing area Beijing area (geographic region), customers (entity type)
[0040] The key entities identified by the query statement will ultimately be mapped to specific field values in the structured query instruction, such as client_name = 'Zhang San', region = 'Beijing', etc.
[0041] The query intent refers to the core query operation or task goal that the user wants the server to complete. It determines the type of database query instruction, the target table, the aggregation method, etc., and is the logical core for generating structured query instructions.
[0042] For example, the query intent obtained by parsing the query statement is shown in Table 2 below:
[0043] Table 2: Examples of query intents obtained from query statements
[0044] Query Intent Example statement Corresponding operation Query list Check all of Zhang San's contracts. SELECT* Statistical Summary Statistics on last month's sales SELECT SUM() Data filtering Filter incomplete orders WHERE status! = 'Completed' Trend Analysis Analyze the sales growth trend over the past three months GROUP BY + Time View details View customer Zhang San's last payment record ORDER BY time DESC LIMIT 1
[0045] The intention is identified through semantic parsing models or rule engines, such as verbs like "statistics," "query," and "analysis," and the operation target and method are determined by combining sentence structure.
[0046] During the structured query generation process, the server first identifies the query intent to select a suitable query template, and then fills in the parameters with key entities to generate a complete and executable query statement.
[0047] Then, the server combines the context data of the current session to complete the information in the query request. For example, if the user's previous query was "all contract information of Zhang San's customers", and the current query is "his payment status last month", the server can deduce from the context that "he" refers to "Zhang San" and complete the key entity.
[0048] Next, based on the parsed query intent and key entities, the server selects a structured template from a pre-defined query template library that matches the query intent. The structured template includes field placeholders, logical conditions, and the syntax format of the target database. For example, the following SQL template might be matched:
[0049] {SELECT SUM(amount)FROM sales_table
[0050] WHERE cl ient_name = '{customer name}' AND month = '{time}';
[0051] The server replaces "Zhang San" in the "{client name}" field and parses "last month" into a specific time range (such as "June 2025"), thus completing the generation of the final structured query command.
[0052] In some embodiments, the query template library can be preset by developers and supported for on-demand expansion. Alternatively, it can be dynamically learned through model training to learn commonly used query patterns of users, thereby achieving automatic template completion and optimization.
[0053] Finally, the generated structured query command will be sent to the target database execution module to complete the query operation and return the result.
[0054] Understandably, the server pre-builds a query template library, with each template corresponding to a specific query intent (such as list queries, aggregation statistics, conditional filtering, etc.). Placeholder fields are set within the templates for later population of key entities. An example is shown below:
[0055] {Template number: T001;}
[0056] Query intent: To calculate sales figures;
[0057] Template content:
[0058] SELECT SUM({field: sales amount}) FROM {table name: sales table}
[0059] WHERE {field:customer name} = '{parameter:customer name}' AND {field:time} = '{parameter:time range}'}
[0060] This query template uses placeholder syntax, meaning that the query statement needs to be dynamically filled by the semantic parsing results when it is generated.
[0061] The query template library can be managed using structured configuration files (such as JSON or YAML) or database tables, and supports indexing by intent to improve retrieval efficiency.
[0062] The semantic parsing module organizes user query intent and key entities into a standardized structure, as shown in the example below:
[0063]
[0064] Based on the above structured results, the server performs the following steps:
[0065] Template matching: Find matching templates in the template library based on the query_intent field (e.g., “Statistics on sales revenue” → template T001); Field mapping confirmation: Confirm the one-to-one correspondence between template fields (e.g., sales amount) and actual fields in the database (e.g., sales_amount) based on the target database field definitions; Parameter filling: Fill key entities (e.g., “Zhang San”, “June 2025”) into the corresponding placeholder positions in the template to obtain the final structured query instruction.
[0066] {SELECT SUM(sales_amount)FROM sales_table
[0067] WHERE client_name='Zhang San'AND sale_month='2025-06';}
[0068] In some embodiments, the server will also automatically adjust syntax details, such as date format and keyword escaping, based on the database type (such as MySQL, PostgreSQL, Oracle) to ensure that the statements are compatible with the selected target database.
[0069] In some examples, key entities and query intents are supplemented based on session context data, including: retrieving historical query requests and supplementing key entities and query intents based on historical query requests and session context.
[0070] In some examples, to improve the natural language query server's ability to understand users' ambiguous expressions, the server, in addition to completing query elements based on session context data, further introduces a long-term memory mechanism to supplement and confirm key entities and query intent by calling the user's historical query requests.
[0071] Specifically, the server maintains a user query log database, recording each user's historical natural language query requests and their parsing results, and indexing them in chronological order. Whenever a user initiates a new natural language query, the server synchronously extracts the session context information of the current query (such as the previous request in the current conversation) and performs the following steps:
[0072] Retrieve historical query requests: The server retrieves the record of the user's last N query requests based on the current user's identifier (such as user ID). The record includes the original query statement, the parsed key entities, the query intent, the query template number, etc.
[0073] The server integrates context and historical information for completion: It combines ambiguous expressions extracted from the current session (such as "he," "last time," "continue searching for that customer") with explicit expressions from historical query requests (such as "Zhang San," "sales volume," "contract information") to complete key entities in the current query. Simultaneously, if the current query intent is incomplete or semantically ambiguous (e.g., "check contracts" could mean viewing a contract list or checking contract status), the server can combine this with the user's past intents with similar expressions to determine which query template the current query is more likely to correspond to, thereby completing or correcting the intent recognition results.
[0074] Constructing structured query instructions: After completing the key entities and query intent, the server determines the target query template based on the completed query intent, fills the completed parameters into the target query template, and generates structured query instructions for subsequent query execution modules to call.
[0075] To illustrate, if a user previously queried "Zhang San's sales last month" and now submits a request "What contracts of his are still incomplete", the server can analyze the content of historical requests to identify that "he" still refers to "Zhang San", and combine this with the query template preferences in the history (such as multiple mentions of contract status) to complete the intent of the current query as "filter Zhang San's incomplete contracts".
[0076] This implementation significantly improves the query server's ability to handle ellipsis, context-dependent expressions, and expressions that follow query habits, expanding the applicability of traditional natural language query servers based on single-round semantic analysis.
[0077] In some examples, before determining the target query template based on the supplemented query intent, the method further includes: vectorizing the query request to obtain the corresponding query vector; identifying similar query requests corresponding to the query vector in a preset query corpus; and correcting the key entities and query intent corresponding to the query request based on the query intent and key entities corresponding to the similar query requests.
[0078] Understandably, this application aims to enhance the completeness of key entity identification and the accuracy of query intent parsing. By introducing a semantic completion mechanism for similar query requests, the server can generate accurate structured query instructions even when the current request is incomplete, semantically ambiguous, or uses habitual language, thus improving the server's fault tolerance and robustness towards natural language queries.
[0079] In some embodiments, the server further supplements or confirms key entities and query intents based on similar query requests. Specifically, the server first vectorizes the current natural language query request to obtain a query vector representing its semantic features; this query vector can be generated by a pre-trained semantic embedding model (such as BERT, Word2Vec, or other language models). Subsequently, the server retrieves one or more historical query requests from a preset query corpus whose similarity to the currently obtained query vector is higher than a preset threshold as similar query requests, wherein the query corpus includes recorded historical query requests and their corresponding key entities and query intents.
[0080] The server employs methods such as cosine similarity to calculate the semantic similarity between the current query vector and historical query request vectors. If a historical query request has a similarity exceeding a set threshold, it is treated as a similar query request. Then, the known key entities and query intents from these similar query requests are used as supplementary information, merging the current session context and historical query data to further enhance the completeness of key entity identification and the accuracy of query intent parsing. By introducing a semantic completion mechanism for similar query requests, the server can still generate accurate structured query instructions even when the current request is incomplete, semantically ambiguous, or uses habitual language, improving the server's fault tolerance and robustness towards natural language queries.
[0081] In some examples, querying the target database according to a structured query instruction and obtaining query results includes: querying the target database according to the structured query instruction; if the target database returns an error message indicating that the structured query instruction was not executed correctly; determining the error type corresponding to the error message and modifying the structured query instruction based on the error type to obtain a new structured query instruction; and querying the target database according to the new structured query instruction to obtain query results.
[0082] Specifically, during the process of querying the target database based on a structured query command, if the target database returns an error message indicating that the structured query command failed to execute, the server can further perform an error analysis and correction process to improve the robustness and accuracy of the query. Specifically, the server can first determine the corresponding error type based on the error message returned by the target database, such as syntax error, missing field, data type mismatch, or insufficient permissions. Then, based on the error type and the structure and semantics of the current query command, the server automatically corrects the structured query command and generates an updated structured query command.
[0083] The correction process may include: adjusting field names in the query statement to match the actual database structure, completing missing query conditions, correcting field type conversion errors, or re-authorizing access to restricted table entries. The corrected structured query will be resubmitted to the target database to retrieve the desired query results. This method effectively avoids query failures caused by incorrect commands, improving the fault tolerance of natural language query servers and the user experience.
[0084] In some specific examples, when the server executes a structured query to access the target database, it first monitors the return results of the target database in real time through an exception handling mechanism (such as a try-catch structure). If the query execution fails, the corresponding error message is immediately captured. Next, the server performs semantic analysis on the error message based on preset error classification rules to identify the error type, such as syntax error, incorrect table name, non-existent field name, data type conflict, restricted permissions, etc.
[0085] After identifying the specific error type, the server will use that error type as a search criterion to look for successful solutions to similar errors in a pre-built error knowledge base. This knowledge base can consist of manually annotated error repair pairs or experience records automatically accumulated by the server during operation. For example, if the query statement contains the "user_name" field and the target database reports an error "column does not exist," the server may identify "username" as the correct field name through the knowledge base and generate a correction suggestion accordingly.
[0086] Subsequently, the server inputs the current natural language query, the failed structured query command, the error message returned by the database, and the corresponding knowledge base repair plan as a comprehensive context into the Large Language Model (LLM). Through imperative prompts, it generates a corrected structured query command. This query statement will adapt and correct the original command in terms of syntax, semantics, and field structure.
[0087] Finally, the server resubmits the generated corrected structured query command to the target database to execute the query operation, and records the effectiveness of the repair process based on the query execution results.
[0088] If an error message is encountered when querying the target database using the new structured query command, the server will attempt other corrective measures based on a preset number of retries. If the problem persists, the server will return a clear failure message to the user, rather than a vague database error.
[0089] In some examples, the database query method also includes recording the correspondence between query requests, structured query instructions, and error messages to update the knowledge base for identifying key entities and query intents.
[0090] Specifically, to improve the accuracy of the server's parsing of natural language query requests, when the server captures an error message returned during the execution of a structured query instruction generated from a query request, it stores the natural language query request, the failed structured query instruction, the corresponding error type, and the correction scheme together. This storage process may include generating training samples containing a triple of "query intent-error pattern-correction result" and adding them to the key entity and query intent recognition knowledge base.
[0091] Furthermore, to support the structured accumulation of knowledge, the server can also introduce a version management mechanism to merge, deduplicate, and optimize existing records in the knowledge base. For example, when multiple similar natural language query requests fail due to the same incorrect field name, the server can use clustering algorithms to uniformly summarize the reasons for the errors and extract more representative correction suggestions.
[0092] When parsing new natural language query requests in the future, the server can combine the current context with the historical requests and repair processes recorded in the knowledge base, and use methods such as association matching or vector similarity calculation to assist in the identification of key entities and the understanding of query intent, thereby improving the server's parsing accuracy and robustness.
[0093] Through this closed-loop mechanism of "error message recording - knowledge update - intent completion", the server can continuously evolve and learn on its own, thereby achieving a more efficient and intelligent natural language query parsing process.
[0094] In one example, if the target database returns an error message during the execution of a structured query, such as "column name does not exist" or "table not defined," the server will perform the following actions to update the knowledge:
[0095] First, the server records the current natural language query request, the structured query instructions (such as SQL statements) generated by that request, and the error messages returned by the database. This recording process can be implemented through logs or query history, and the above content can be stored in a pre-defined query knowledge base.
[0096] The server then uses the recorded data as training samples to update the entity recognition rules for queries or the knowledge base. Specifically, the server analyzes the relationship between the key fields involved in the error message and the original query request, and combines this with the corrected structured query instructions to extract new entity terms, field aliases, or intent templates, thereby enhancing the model's ability to handle similar query requests.
[0097] Through the above process, when a natural language query request with a similar semantic structure is received subsequently, the server can automatically complete or correct key entities and query intent based on historical correction records, thereby improving the accuracy and robustness of generating structured query instructions.
[0098] All of the above-mentioned optional technical solutions can be combined in any way to form the optional embodiments of this application, and will not be described in detail here.
[0099] The following are embodiments of the apparatus described in this application, which can be used to execute the embodiments of the method described in this application. For details not disclosed in the apparatus embodiments of this application, please refer to the embodiments of the method described in this application.
[0100] Based on the same concept, this application also provides a database query device, such as... Figure 2 As shown, the database query device includes:
[0101] The determination module 201 is used to receive a query request from a user based on natural language input, and determine the target database from the database set based on the query request;
[0102] The generation module 202 is used to obtain the session context data corresponding to the query request, and generate a structured query instruction for querying the target database based on the session context data and the query request.
[0103] The query module 203 is used to query the target database according to the query command and obtain the query results.
[0104] In some examples, the generation module 202 is also used to parse key entities and query intents in the query request; supplement the key entities and query intents based on session context data; determine the target query template based on the supplemented query intents; and generate a structured query instruction corresponding to the target database based on the supplemented key entities and target query templates.
[0105] In some examples, the generation module 202 is also used to obtain historical query requests and supplement key entities and query intents based on historical query requests and session context.
[0106] In some examples, the generation module 202 is also used to vectorize the query request to obtain the corresponding query vector; determine similar query requests corresponding to the query vector in the preset query corpus; and modify the key entities and query intents corresponding to the query request based on the query intents and key entities corresponding to the similar query requests.
[0107] In some examples, the query module 203 is also used to query the target database according to the query instruction; if the target database returns an error message indicating that the query instruction was not executed correctly; determine the error type corresponding to the error message, and modify the query instruction based on the error type to obtain a new query instruction; and query the target database according to the new query instruction to obtain the query result.
[0108] In some examples, the query module 203 is also used to record the correspondence between query requests, query instructions, and error messages in order to update the knowledge base for identifying key entities and query intents.
[0109] According to the solution provided in the embodiments of this application, the database query device receives a query request from a user based on natural language input, and determines the target database from the database set based on the query request; obtains the session context data corresponding to the query request, and generates a structured query instruction for querying the target database based on the session context data and the query request; queries the target database according to the query instruction to obtain the query result. This method accurately identifies the query intent and automatically constructs a structured query instruction by combining the user's natural language query request with the current session context data, realizing intelligent database query, simplifying the database query method, improving the database query efficiency, and overcoming the technical problem that existing natural language query servers have difficulty understanding context and handling complex query logic. This significantly reduces the data access threshold for non-technical users, improves the accuracy and automation of the query, and avoids the problem of low data query efficiency caused by complex database query methods in the prior art.
[0110] Figure 3 This is a schematic diagram of the electronic device 3 provided in an embodiment of this application. Figure 3 As shown, the electronic device 3 of this embodiment includes: a processor 301, a memory 302, and a computer program 303 stored in the memory 302 and executable on the processor 301. When the processor 301 executes the computer program 303, it implements the steps in the various method embodiments described above. Alternatively, when the processor 301 executes the computer program 303, it implements the functions of each module / unit in the various device embodiments described above.
[0111] Electronic device 3 can be a desktop computer, laptop, handheld computer, cloud server, or other electronic device. Electronic device 3 may include, but is not limited to, processor 301 and memory 302. Those skilled in the art will understand that... Figure 3 This is merely an example of electronic device 3 and does not constitute a limitation on electronic device 3. It may include more or fewer components than shown, or different components.
[0112] The processor 301 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.
[0113] The memory 302 can be an internal storage unit of the electronic device 3, such as a hard disk or RAM. The memory 302 can also be an external storage device of the electronic device 3, such as a plug-in hard disk, Smart Media Card (SMC), Secure Digital (SD) card, or Flash Card. The memory 302 can also include both internal and external storage units of the electronic device 3. The memory 302 is used to store computer programs and other programs and data required by the electronic device.
[0114] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is merely an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiments can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0115] If an integrated module / unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program may include computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. A computer-readable medium may include: any entity or device capable of carrying computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in a computer-readable medium can be appropriately added or removed according to regional requirements and patent practice requirements. For example, in some regions, according to regional requirements and patent practice, a computer-readable medium may not include electrical carrier signals and telecommunication signals.
[0116] The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.
Claims
1. A database query method, characterized in that, The method includes: Receive a query request from a user based on natural language input, and determine the target database from the database set based on the query request; Obtain the session context data corresponding to the query request, and generate a structured query instruction for querying the target database based on the session context data and the query request; The target database is queried according to the structured query instruction to obtain the query results.
2. The method according to claim 1, characterized in that, Based on the session context data and the query request, a structured query instruction for querying the target database is generated, including: Analyze the key entities and query intent in the query request; The key entities and the query intent are supplemented based on the session context data; Based on the supplemented query intent, a target query template is determined, and based on the supplemented key entities and the target query template, a structured query instruction corresponding to the target database is generated.
3. The method according to claim 2, characterized in that, Supplementing the key entities and query intent based on the session context data includes: Obtain historical query requests, and supplement the key entities and query intent based on the historical query requests and the session context.
4. The method according to claim 2, characterized in that, Before determining the target query template based on the supplemented query intent, the method further includes: The query request is vectorized to obtain the corresponding query vector; In a pre-defined query corpus, identify similar query requests corresponding to the query vector; Based on the query intent and key entities corresponding to the similar query requests, the key entities and query intent corresponding to the query requests are modified.
5. The method according to claim 1, characterized in that, The target database is queried according to the structured query instruction to obtain query results, including: The target database is queried according to the structured query instruction. If the target database returns an error message indicating that the structured query instruction was executed incorrectly; Determine the error type corresponding to the error message, and modify the structured query instruction based on the error type to obtain a new structured query instruction; The target database is queried according to the new structured query instruction to obtain the query results.
6. The method according to claim 5, characterized in that, The method further includes: Record the correspondence between the query request, the structured query instruction, and the error message to update the key entity and query intent identification knowledge base.
7. A database query device, characterized in that, The device includes: The determination module is used to receive a query request from a user based on natural language input, and determine the target database from the database set based on the query request; The generation module is used to obtain the session context data corresponding to the query request, and generate a structured query instruction for querying the target database based on the session context data and the query request. The query module is used to query the target database according to the structured query instructions and obtain query results.
8. The apparatus according to claim 7, characterized in that, The generation module is also used to parse the key entities and query intent in the query request; The key entities and the query intent are supplemented based on the session context data; a target query template is determined based on the supplemented query intent; and a structured query instruction corresponding to the target database is generated based on the supplemented key entities and the target query template.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method as described in any one of claims 1 to 6.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 6.
Citation Information
Cited By
Data query method, device and equipment based on APaaS platform and medium
CN121501969A
