A natural language query method and system for rail transit field
By combining ElasticSearch and Milvus databases with rail transit expert knowledge to generate MQL and SQL statements, the adaptation problem of general NL2SQL technology in the rail transit field has been solved, realizing accurate, safe and efficient natural language query services, and improving the quality of table selection and SQL generation.
Patent Information
- Application Number
- CN202511508207.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-22
- Publication Date
- 2026-01-27
- Estimated Expiration
- 2045-10-22
AI Technical Summary
When general NL2SQL technology is adapted to the rail transit field, it suffers from problems such as user business problem misunderstanding, lack of user permission verification, inefficient table selection, and low SQL generation quality. As a result, non-technical personnel cannot obtain accurate, secure, and efficient natural language query services.
ElasticSearch and Milvus vector databases are used for content and semantic matching. Combined with a user permission dictionary and basic indicator library of experts in the rail transit field, MQL statements are generated through a large model and finally SQL statements are generated. Rail transit role permission verification logic is embedded to build candidate data tables. Graph convolution algorithm is used to optimize table selection and data query is performed through a thesaurus.
It enables accurate, safe, and efficient natural language queries in the rail transit field, ensures data access security, improves the accuracy of table selection and the quality of SQL generation, and solves the adaptation defects of general NL2SQL technology in the rail transit field.
Smart Images

Figure CN120994693B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data query, and in particular to a natural language query method and system for the rail transit field. Background Technology
[0002] With the development of the big data era, the rail transit industry has an increasingly urgent need for data querying and analysis. For example, maintenance personnel need to quickly obtain rail transit fault data, and managers need to statistically analyze peak passenger flow characteristics. These needs require data query systems to understand rail transit business terminology and be able to link data across systems. To lower the query threshold for non-technical personnel in the rail transit field, the technology of converting natural language to structured query language (NL2SQL) has been introduced. However, general NL2SQL technology has revealed significant domain adaptation deficiencies when adapted to rail transit scenarios. Specific problems are as follows:
[0003] Deficiencies in understanding user business issues: The rail transit sector has strong business attributes, and the business indicator dictionary on which general NL2SQL technology relies usually lacks coverage of rail transit-specific indicators such as fault response time and at-fault failure rate.
[0004] Lack of User Permission Verification: In the rail transit sector, queries must strictly adhere to 'permission-data' matching rules. For example, dispatchers should only be able to query operational data, not ticketing data. General NL2SQL technology typically lacks fine-grained permission control mechanisms, making it difficult to impose access restrictions on corresponding data based on user roles, which can easily lead to unauthorized queries and the leakage of sensitive information.
[0005] Inefficient table selection: Rail transit data is scattered across multiple independent systems such as operation, signaling, and maintenance, resulting in complex and diverse table data. General NL2SQL technology often fails to fully utilize the relationships between table data and struggles to automatically learn the join patterns between tables, leading to inaccurate table selection.
[0006] Low-quality SQL generation: The query logic in the rail transit field is complex, and general NL2SQL technology often cannot accurately understand the deep business semantics, resulting in SQL statements with syntax errors, mismatches of proprietary fields, and other problems, which affect the accuracy and usability of query results. Summary of the Invention
[0007] The purpose of this application is to provide a natural language query method and system for the rail transit field, in order to solve the problems of general NL2SQL technology when adapted to rail transit scenarios, such as user business problem misunderstanding, lack of user permission verification, inefficient table selection, and low SQL generation quality, which make it impossible to provide accurate, secure and efficient natural language query services for non-technical personnel.
[0008] To achieve the above objectives, this application provides the following solution.
[0009] Firstly, this application provides a natural language query method for the field of rail transit, including the following steps.
[0010] Based on the ElasticSearch database and Milvus vector database, content matching and semantic matching are performed on the input user query to determine the first decomposition recall data and the second decomposition recall data.
[0011] Based on the user permission dictionary, a candidate data table is constructed according to the first decomposed recall data and the second decomposed recall data; the user permission dictionary is an access permission dictionary for different roles constructed by experts in the field of rail transit.
[0012] Based on the content-field data tables of the ElasticSearch database and the Milvus vector database, the first aligned recall data corresponding to the user query question is determined, and entities in the user query question are extracted using a large model combined with prompt word templates.
[0013] For each entity, semantic matching is performed on each entity based on the Milvus vector database to determine the second aligned recall data. Based on the description information of the candidate data table, the fields in the second aligned recall data are mapped to the same language as the user query and determined as the third aligned recall data.
[0014] Based on the basic indicator library, MQL statements are constructed according to the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data. The basic indicator library is constructed by domain experts from relevant documents in the rail transit field, summarizing rail transit-specific indicators and corresponding calculation logic. The rail transit-specific indicators include fault response time and at-fault rate. The relevant documents in the rail transit field include rail transit operation and maintenance logs, operation reports, and industry standard documents. The basic indicator library is dynamically updated based on a set interval, and the MQL statement includes the calculated indicator, the involved dimensions, and the calculation logic.
[0015] Based on the user's query question, SQL generation sample data is obtained from the query-SQL data table in the ElasticSearch database and the Milvus vector database.
[0016] Based on the SQL sample data, the MQL statement, and the candidate data table, the large model is invoked, and the first SQL statement is generated in conjunction with the prompt word template.
[0017] Data is queried based on a thesaurus of search criteria constructed by experts in the field of rail transit and the first SQL statement, and the data of the entire interaction process is written into a MySQL database; each line of the thesaurus includes the current word, context and mapping word.
[0018] Secondly, this application provides a natural language query system for the field of rail transit, including the following modules.
[0019] The content matching and semantic matching module is used to perform content matching and semantic matching on the input user query based on the ElasticSearch database and Milvus vector database to determine the first decomposition recall data and the second decomposition recall data.
[0020] The candidate data table construction module is used to construct a candidate data table based on the user permission dictionary, according to the first decomposed recall data and the second decomposed recall data; the user permission dictionary is an access permission dictionary for different roles constructed by experts in the rail transit field.
[0021] The entity extraction module is used to determine the first aligned recall data corresponding to the user query question based on the content-field data table of the ElasticSearch database and the Milvus vector database, and to extract entities from the user query question using a large model combined with prompt word templates.
[0022] The mapping module is used to perform semantic matching on each entity based on the Milvus vector database to determine the second aligned recall data, and to map the fields in the second aligned recall data to the same language as the user query according to the description information of the candidate data table, and determine them as the third aligned recall data.
[0023] The MQL statement construction module is used to construct MQL statements based on a basic indicator library, the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data. The basic indicator library is constructed by domain experts from relevant documents in the rail transit field, summarizing rail transit-specific indicators and corresponding calculation logic. The rail transit-specific indicators include fault response time and at-fault rate. The relevant documents in the rail transit field include rail transit operation and maintenance logs, operation reports, and industry standard documents. The basic indicator library is dynamically updated at set intervals, and the MQL statement includes calculated indicators, involved dimensions, and calculation logic.
[0024] The SQL sample data acquisition module is used to acquire SQL sample data from the query-SQL data table in the ElasticSearch database and the Milvus vector database based on the user's query question.
[0025] The first SQL statement generation module is used to generate sample data, the MQL statement, and the candidate data table based on the SQL, call the large model, and generate the first SQL statement in combination with the prompt word template.
[0026] The data query module is used to perform data queries based on a thesaurus of synonyms with search conditions constructed by experts in the field of rail transit and the first SQL statement, and write the entire interactive process data into a MySQL database; each line of the thesaurus includes the current word, context, and mapped word.
[0027] According to the specific embodiments provided in this application, this application has the following technical effects: Based on the ElasticSearch database (i.e., ES database) and Milvus vector database, this application performs content matching and semantic matching on the input user query questions, and constructs a dynamically updated basic indicator library by manually summarizing proprietary indicators and corresponding calculation logic in the rail transit field from relevant documents, thereby better adapting to the understanding needs of rail transit field terminology.
[0028] Secondly, this application incorporates a user permission dictionary and embeds rail transit role permission verification logic to ensure data access security; and improves the efficiency and accuracy of table selection by constructing a candidate data table.
[0029] Finally, this application uses the content-field data tables of the ElasticSearch database and the Milvus vector database to determine the first aligned recall data corresponding to the user query question. It then uses a large model combined with prompt word templates to extract entities from the user query question. For each entity, it determines the MQL statement, obtains SQL generation sample data, and finally obtains the first SQL statement. It then uses a thesaurus to perform data query. The entire interaction process is a "semantic parsing - structured indicators - compliance verification - intelligent reflection" mechanism, which optimizes the quality of SQL generation.
[0030] In summary, this application can effectively solve the adaptation defects of general NL2SQL technology in the field of rail transit, and provide accurate, safe and efficient natural language query services for non-technical personnel. Attached Figure Description
[0031] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the embodiments 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.
[0032] Figure 1 This is a flowchart illustrating a natural language query method for the rail transit field provided in an embodiment of this application.
[0033] Figure 2 This is a schematic diagram of the technical implementation steps provided in an embodiment of this application. Detailed Implementation
[0034] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0035] To make the objectives, features and advantages of this application more apparent and understandable, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0036] like Figure 1 As shown in the figure, this application provides a natural language query method for the field of rail transit, including the following steps.
[0037] S1: Based on the ElasticSearch database and Milvus vector database, perform content matching and semantic matching on the input user query to determine the first decomposition recall data and the second decomposition recall data.
[0038] S2: Based on the user permission dictionary, construct a candidate data table according to the first decomposition recall data and the second decomposition recall data; the user permission dictionary is an access permission dictionary for different roles constructed by experts in the field of rail transit.
[0039] S3: Based on the content-field data tables of the ElasticSearch database and the Milvus vector database, determine the first aligned recall data corresponding to the user query question, and use the large model combined with the prompt word template to extract entities from the user query question.
[0040] S4: For each entity, based on the Milvus vector database, perform semantic matching on each entity to determine the second aligned recall data, and according to the description information of the candidate data table, map the fields in the second aligned recall data to the same language as the user query, and determine them as the third aligned recall data.
[0041] S5: Based on the basic indicator library, construct MQL statements according to the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data; the basic indicator library is constructed by domain experts from relevant documents in the rail transit field, summarizing rail transit-specific indicators and corresponding calculation logic; the rail transit-specific indicators include fault response time and at-fault rate; the relevant documents in the rail transit field include rail transit operation and maintenance logs, operation reports, and industry standard documents; the basic indicator library is dynamically updated based on a set interval, and the MQL statement includes calculated indicators, involved dimensions, and calculation logic.
[0042] S6: Based on the user query question, obtain SQL generation sample data from the query-SQL data table in the ElasticSearch database and the Milvus vector database.
[0043] S7: Generate sample data based on the SQL, the MQL statement, and the candidate data table, call the large model, and generate the first SQL statement in conjunction with the prompt word template.
[0044] S8: Perform data query based on the thesaurus of search conditions constructed by experts in the field of rail transit and the first SQL statement, and write the entire interactive process data into the MySQL database; each line of the thesaurus includes the current word, context and mapping word.
[0045] In one exemplary embodiment, S1 specifically includes the following steps.
[0046] S11: Based on the ElasticSearch database query decomposition data table, perform content matching on the input user query to determine the first decomposition recall data.
[0047] S12: Based on the query decomposition data table of the Milvus vector database, perform semantic matching on the input user query question to determine the second decomposition recall data.
[0048] In one exemplary embodiment, S2 specifically includes the following steps.
[0049] S21: Based on the rearrangement model, the first decomposed recall data and the second decomposed recall data are fused and reordered according to their relevance to determine the third decomposed recall data.
[0050] S22: Integrate the third decomposition recall data into the configured prompt word template, and call the large model to obtain the decomposition results and fine-grained query content of the user query.
[0051] S23: Based on the fine-grained query content and the access permission dictionary, determine whether the query content in the user query question exceeds the current user's query data range. If yes, execute S24; otherwise, execute S25.
[0052] S24: Displays the range of data that the current user can query, and displays a prompt to re-enter the data.
[0053] S25: Select table data based on the user query question and construct a candidate data table.
[0054] In one exemplary embodiment, S5 specifically includes the following steps.
[0055] S51: Statistically analyze the usage frequency of each field in SQL queries within historical time periods, combine the third alignment recall data, use the large model and the prompt word template to filter search conditions and search fields, and use the search results as the fourth alignment recall data.
[0056] S52: Perform reverse mapping based on the description information of the candidate data table and the field names of the fourth aligned recall data to determine the fifth aligned recall data.
[0057] S53: Based on the basic indicator library, the fine-grained query content, and the fifth alignment recall data, a structured MQL statement is generated using a large model and in conjunction with the prompt word template.
[0058] In one exemplary embodiment, S7 specifically includes the following steps.
[0059] S71: Based on the decomposition results of the user query, the DDL information of the candidate data table, the fifth alignment recall data, the MQL statement, and the SQL generation sample data, call the large model and generate the first SQL statement in combination with the prompt word template.
[0060] In one exemplary embodiment, S8 specifically includes the following steps.
[0061] S81: Input the first SQL statement into the SQLGlot parser for syntax analysis and verification, and determine the verification result; the verification includes checking the integrity of JOIN and verifying the legality of aggregate functions.
[0062] S82: If the verification result is an error found, record the error type and the location of the error, and provide feedback on the error information, and update the error count.
[0063] S83: Determine whether the number of errors is less than the maximum number of attempts. If yes, return "Based on the decomposition results of the user query, the DDL information of the candidate data table, the fifth alignment recall data, the MQL statement, and the SQL generation sample data, call the large model and generate the first SQL statement in combination with the prompt word template". If no, set the query status to failure.
[0064] S84: If the verification result is no error found, based on the thesaurus, the search conditions in the first SQL statement are replaced with synonyms to generate a second SQL statement.
[0065] S85: Execute the second SQL statement.
[0066] S86: If an error occurs during execution, record and report the error information, and update the error count.
[0067] S87: If the execution is successful, return the query results to the user interface and set the query status to success.
[0068] Based on the above methods, such as Figure 2 As shown, the specific implementation steps of this application are as follows.
[0069] Step 1: User inputs natural language query question.
[0070] Steps 2-4 involve retrieving data from the query table using Elasticsearch and Milvus, and then re-ranking the results.
[0071] In step 2: Based on the user query question input in step 1, use formula (1) to perform fast content matching in the query decomposition data table of the ElasticSearch database. Recall N data points from high to low according to the content matching score, which are denoted as the first decomposition recall data. N is usually 1-5.
[0072]
[0073] in, This represents the content matching score; Q represents the user's query question, and D represents the document in the ElasticSearch database. Represents the first in the user's query question One word, represent The number of times it appears in document D, represent Scarcity among all documents Represents document length. Represents the average length of all documents. and Represents a regulatory factor.
[0074] Step 3: Based on the user query question input in Step 1, semantic matching is performed in the query decomposition data table of Milvus vector database using formula (2). N data are recalled from high to low according to the semantic matching score, which is recorded as the second decomposition recall data. N is usually consistent with N in Step 2.
[0075]
[0076] in, This refers to the semantic matching score; These represent the user's query question and the document in the Milvus vector database, respectively. This represents a multi-dimensional vector derived from a vectorized model (such as BGE-M3). Represents the magnitude of a vector.
[0077] Step 4: Input the first decomposition recall data from Step 2 and the second decomposition recall data from Step 3 into the re-ranking model (such as BGE-Rerank-v2-m3). Merge and re-rank the recall data from Step 2 and Step 3. Recall N data points from high to low based on the relevance score. These are denoted as the third decomposition recall data. N is usually consistent with N in Step 2.
[0078] Step 5: The large model decomposes the user input, specifically including: integrating the third decomposition recall data from Step 4 into the configured prompt word template, and using the large model to obtain the decomposed results and fine-grained query content of the user query.
[0079] Step 6: Domain experts construct access permission dictionaries for different roles, i.e., user permission dictionaries.
[0080] Step 7: User permission determination, specifically including: comparing the fine-grained query content in Step 5 with the user access permission data dictionary in Step 6. If the user's query content contains content that does not belong to the user access permission data dictionary, the range of data that the user can query is displayed and the user is prompted to re-enter the data. If the user's query content conforms to the user access permission data dictionary, proceed to Step 8.
[0081] Compared to general NL2SQL technical solutions, this application can achieve data access security: by embedding rail transit role permission verification logic, strictly following the "permission-data" matching rules, it can accurately restrict users to accessing only data within the corresponding permission scope, effectively solving the problem of missing permission verification in general technology, preventing unauthorized queries and leakage of sensitive information, and ensuring data security.
[0082] Step 8: Select table data based on the user query question input in Step 1. When the number of data tables from different sources is less than 20, the table name, field name, and field description of each table are regarded as a complete document. The BM25 score of each table and the user input is calculated using formula (1), and the table with a score higher than 0.8 is retained as the candidate data table. When the number of data tables from different sources is greater than 20, each table is abstracted as a node on the graph. If there is a foreign key relationship between the tables, an undirected edge related to the foreign key type is established between the two nodes. The vectorized representations of different nodes and edges are randomly initialized, and the graph convolution algorithm and deep learning framework are used for training to obtain the final representations of nodes and edges. The graph convolution process is shown in formula (3), the learning objective of deep learning is shown in formula (4), and the predicted score of the candidate data table is shown in formula (5).
[0083]
[0084]
[0085]
[0086] in, The central node represents, represent The set of neighboring nodes, Representing neighbor nodes The importance of. The predicted score represents the candidate data table. M represents the true score of the candidate data table, M represents the number of candidate data tables, and X is the vector representation of the user's query question.
[0087] During the training phase, the user-input question vector is not updated; only the relevant parameters of the graph convolutional network are updated. During the prediction phase, the trained graph convolutional network model is loaded, and the score of each candidate data table is obtained. Tables with scores higher than 0.8 are retained as candidate data tables.
[0088] Compared with the general NL2SQL technical solution, this application has an intelligent and efficient table screening strategy: by combining statistical metrics and graph algorithms to mine the correlation relationships of table data in multiple independent systems such as the operation system and the signal system, automatically learning the table connection patterns, getting rid of the limitations of the general technology that selects tables relying on single information and cannot utilize the correlations between tables, achieving fast and accurate table selection in complex scenarios, reducing the problem of invalid queries caused by incorrect table selection, and improving the query efficiency.
[0089] Step 9: Retrieve and Rerank in the ES and Milvus content-field tables, specifically including: Based on the user query problem input in Step 1, in the content-field data tables of the ElasticSearch database and the Milvus database, following specific conditions (such as restricting field names), and following the processes in Step 2, Step 3, and Step 4, obtain the first aligned recall data.
[0090] Step 10: Entity recognition and extraction, specifically including: Based on the user query problem input in Step 1 and the first aligned recall data in Step 9, use a large model combined with prompt templates to extract possible entities in the user query.
[0091] Step 11: Retrieve and Rerank in the Milvus content-field table, specifically including: Based on each entity extracted in Step 10, perform semantic matching in the content-field data table of the Milvus vector database using formula (2), recall N data in descending order of scores, denoted as the second aligned recall data, and N usually remains consistent with N in Step 2.
[0092] Step 12: Field name mapping, specifically including: Based on the description information of the candidate data tables in Step 8, map the fields in the second aligned recall data to the same language as the user query, denoted as the third aligned recall data. For example, convert the field "line_name" to the Chinese "线路名称" for semantic matching with the user query "线路".
[0093] Step 13: Screening and filtering by the large model, specifically including: Statistically analyze the usage frequency of each field in SQL queries within the past 30 days, combine with the third aligned recall data in Step 12, and use the large model and prompt templates to complete the screening of retrieval conditions and retrieval fields. The result is denoted as the fourth aligned recall data.
[0094] Step 14: Reverse field name mapping, specifically including: Based on the description information of the candidate data tables in Step 8, reverse map the field names in the fourth aligned recall data in Step 13 from the user query language type to the original type, such as mapping "线路名称" to "line_name", and the result is denoted as the fifth aligned recall data.
[0095] Step 15: Business Indicator Dictionary. This includes: Domain experts manually summarizing rail transit-specific indicators such as fault response time and at-fault rate, along with their corresponding calculation logic, from documents such as rail transit operation and maintenance logs, operational reports, and industry standards, to build a basic indicator library. Every week, user-submitted indicators and calculation logic undergo manual review; approved indicators and calculation logic are used to update the indicator library.
[0096] Compared to general NL2SQL technical solutions, this application constructs a dynamically updated business dictionary, namely a basic indicator library, which includes rail transit-specific indicators such as fault response time and at-fault rate. This compensates for the lack of coverage of general technical business indicators and can accurately capture domain terms in user business questions. When non-technical personnel ask questions using professional expressions, the system can accurately understand the requirements and avoid query deviations caused by misunderstandings of terminology.
[0097] Step 16: MQL statement, specifically including: based on the fine-grained query content in Step 5, the fifth alignment recall data in Step 14, the indicators and calculation logic of the basic indicator library in the transportation field in Step 15, and using the large model combined with prompt word templates to generate a structured indicator query language (MQL). The MQL explicitly includes the calculated indicators, the dimensions involved, and the calculation logic.
[0098] Step 17: Retrieve and Rerank SQL Tables in Elasticsearch and Milvus. Specifically, based on the user query question entered in Step 1, retrieve sample SQL data from the query-SQL data tables in Elasticsearch and Milvus databases, following the processes in Steps 2, 3, and 4.
[0099] Step 18: The first SQL statement, specifically including: based on the user query decomposition content in Step 5, the DDL information of the candidate data table in Step 8, the fifth alignment recall data in Step 14, the structured indicator query language in Step 16, and the SQL generation sample data in Step 17, the first SQL statement is generated by calling the large model and combining it with the prompt word template.
[0100] Step 19: Thesaurus, which specifically includes: a thesaurus mapping search criteria constructed by domain experts. Each row of the thesaurus contains three attributes: the current word, the context, and the mapped word.
[0101] Step 20: SQL parsing, specifically including: inputting the first SQL statement generated in Step 18 into the SQLGlot parser, performing syntax analysis on the SQL statement, checking for syntax errors such as misspelled keywords and punctuation errors. Checking JOIN integrity to ensure the join conditions between tables are correct. Verifying the validity of aggregate functions, checking whether the parameters of aggregate functions are correct and used in the appropriate context. If an error is found, recording the error type and location, and returning the error message to proceed to Step 23; otherwise, proceed to Step 21.
[0102] Step 21: The second SQL statement specifically includes: based on the thesaurus in step 19, performing synonym replacement on the search conditions in the first SQL statement in step 18 to generate the second SQL representation.
[0103] Step 22: SQL execution, specifically including: executing the second SQL statement; if an error occurs during execution, record detailed error information, including the error type (such as data type mismatch) and the location where the error occurred (such as the specific SQL statement fragment), and proceed to step 23; otherwise, proceed to step 24.
[0104] Step 23: Determine if the maximum number of attempts has been reached. Specifically, update the error count. If the error count is less than the maximum number of attempts, proceed to step 18; otherwise, proceed to step 25.
[0105] Step 24: Set the query status to successful, which includes: returning the query results in the user interface and recording the query status as successful.
[0106] Step 25: Set the query status to failure, specifically including: recording the query status as failure.
[0107] Step 26: Input NL2SQL process information into the MySQL database. This includes writing the entire interaction process data into the MySQL database for subsequent statistics and analysis.
[0108] Compared to general NL2SQL technical solutions, this application has a comprehensive quality assurance system: it designs a full-process mechanism of "semantic parsing - structured indicators - compliance verification - intelligent reflection", which can effectively avoid problems such as syntax errors in general SQL statements and matching errors in proprietary fields, thereby improving system stability and availability.
[0109] The technical solution of this application is described below with specific examples, and the specific steps are as follows.
[0110] Step 1): Enter the user's query question: "The failure rate of electromechanical equipment at Dongdaqiao Station on Line 6 in the past three months".
[0111] Step 2): Use the ElasticSearch database to select the example question in the local knowledge base that is most similar to the user input, such as "failure rate of power supply professionals on Line 5 in the past year" and its corresponding decomposition question as the first decomposition recall data.
[0112] Step 3): Use the Milvus vector database, referring to Step 2), as the second decomposition recall data.
[0113] Step 4) An example of the third decomposition recall data is shown below. Here, N=1 is for the sake of simplicity in showing the style of the third decomposition recall data, and it may be different from the N in the following steps.
[0114] {“Recall Query”: “Number of faults on Line 6 in the past six months”, “Query Decomposition Results”: [(1) Calculate the current date and the start date of the past six months. (2) Filter out all records of fault data for “Line 6” that occurred between the start date calculated in step 1 and the current date. (3) Count the results filtered in step 2.]}
[0115] Step 5): {"User input decomposition results": [(1) Calculate the current date and the start date of the last three months. (2) Filter out all records with the line "Line 6", the location "Dongdaqiao Station", the major name "Mechanical and Electrical Engineering", and the occurrence time between the start date calculated in the first step and the current date. (3) Calculate the failure rate of the results filtered in the second step. ], "User input fine-grained query content": ["Line 6", "Last three months", "Dongdaqiao Station", "Mechanical and Electrical Engineering", "Failure Rate"]}.
[0116] Step 6): Domain experts construct access permission dictionaries for different roles, i.e., user permission dictionaries.
[0117] Step 7): When the query input is "failure rate of electromechanical professionals at Dongdaqiao Station on Line 6 in the past three months" and the user's major is not "electromechanical professionals", the user is not allowed to perform this major query according to the permission judgment rules.
[0118] Step 8): Select the operation and maintenance system fault data table as the candidate data table.
[0119] Step 9): Taking the search condition as “line_name” and N=3 as an example, the first aligned recall data is: [“line 6”, “line 5”, “line 7”].
[0120] Step 10): Extracted entities: ["Line 6", "Dongdaqiao Station", "Mechanical and Electrical"].
[0121] Step 11): Taking N=3 as an example, the second alignment recall data corresponding to the entity "Line 6" is: ["Line 6 / line_name", "Line 5 / line_name", "Line 7 / line_name"]; the alignment recall data corresponding to the entity "Dongdaqiao Station" is: ["Dongdaqiao / location_3", "Dongdajie / location_2", "Huaxiangqiao / location_3"]; the alignment recall data corresponding to the entity "Mechanical and Electrical Engineering" is: ["Mechanical and Electrical Engineering / company_short_name", "Mechanical and Electrical Engineering / major_name", "Mechanical Engineering / checkpoint_name"].
[0122] Step 12): Taking N=3 as an example, the third alignment recall data corresponding to the entity "Line 6" is: ["Line 6 / Line Name", "Line 5 / Line Name", "Line 7 / Line Name"]; the alignment recall data corresponding to the entity "Dongdaqiao Station" is: ["Dongdaqiao / Location 3", "Dongdajie / Location 2", "Huaxiangqiao / Location 3"]; the alignment recall data corresponding to the entity "Mechatronics" is: ["Mechatronics / Company Abbreviation", "Mechatronics / Professional Name", "Mechanical / Item Name"].
[0123] Step 13): Fourth aligned recall data: {"Line 6": "Line 6 / Line Name", "Dongdaqiao Station": "Dongdaqiao / Location 3", "Mechanical and Electrical": "Mechanical and Electrical / Professional Name"}.
[0124] Step 14): Fifth aligned recall data: {"Line 6": "Line 6 / line_name", "Dongdaqiao Station": "Dongdaqiao / location_3", "Mechanical & Electrical": "Mechanical & Electrical / major_name"}.
[0125] Step 15): In this part, domain experts will organize documents such as "Operation Logs", "Operation Reports" and "Industry Standards" to build a database of metrics and dimensions.
[0126] Step 16): MQL query language: {"Time dimension": "Last three months", "Location dimension": "Line 6", "Professional dimension": "Mechanical and electrical", "Indicator": "Failure rate", "Indicator calculation method": "Number of responsible failures / Total number of failures"}.
[0127] Step 17): SQL generates sample data: [{"User Query": "Statistics on the response timeliness rate of communication equipment failures on Line 1 in the past month", "SQL": "SELECT COUNT(CASE WHEN response_time<= 30 THEN 1END) * 1.0 / NULLIF(COUNT( ), 0) AS timely_response_rate” WHERE occurrence_time>= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND occurrence_time<= CURDATE()AND line_name = 'Line 1' AND major_name = 'Communication'}]
[0128] Step 18): SELECT
[0129] COUNT(CASE WHEN is_responsible = 1 THEN 1 END) * 1.0 / NULLIF(COUNT(*), 0) AS fault_rate,
[0130] FROM
[0131] fault_records
[0132] WHERE
[0133] occurrence_time>= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
[0134] AND occurrence_time<= CURDATE()
[0135] AND line_name = 'Line 6'
[0136] AND major_name = 'Mechanical and Electrical'
[0137] Step 19): Domain experts build a terminology database and a thesaurus. For example, “comb plate”, “upper comb plate”, and “lower comb plate” are all used as a group of synonyms. The latter two words are used as the current terms, and the corresponding standard term is “comb plate”, which is the mapping word.
[0138] Step 20): Check the generated SQL for syntax errors, such as referencing a non-existent table or missing a semicolon at the end.
[0139] Step 21): Based on step 19, perform synonym replacement, for example, identify the entity "lower comb plate" and replace it with "comb plate".
[0140] Step 22): Record information such as the error and error type that occurred in step 20.
[0141] Step 23): Perform a cyclical reflection based on the maximum number of error attempts. For example, if the current error count is 1 and the error attempt threshold is 3, then continue to call the large model reflection capability to make SQL modifications until the maximum number of error attempts is reached.
[0142] Step 24): Record the query status as successful.
[0143] Step 25): Record the query status as failed.
[0144] Step 26): After completing steps 1-25, regardless of whether the final query status is successful or failed, the entire data interaction process will be written to MySQL for storage.
[0145] This application provides a natural language query system for the rail transit field, which includes the following modules.
[0146] The content matching and semantic matching module is used to perform content matching and semantic matching on the input user query based on the ElasticSearch database and Milvus vector database to determine the first decomposition recall data and the second decomposition recall data.
[0147] The candidate data table construction module is used to construct a candidate data table based on the user permission dictionary, according to the first decomposed recall data and the second decomposed recall data; the user permission dictionary is an access permission dictionary for different roles constructed by experts in the rail transit field.
[0148] The entity extraction module is used to determine the first aligned recall data corresponding to the user query question based on the content-field data table of the ElasticSearch database and the Milvus vector database, and to extract entities from the user query question using a large model combined with prompt word templates.
[0149] The mapping module is used to perform semantic matching on each entity based on the Milvus vector database to determine the second aligned recall data, and to map the fields in the second aligned recall data to the same language as the user query according to the description information of the candidate data table, and determine them as the third aligned recall data.
[0150] The MQL statement construction module is used to construct MQL statements based on a basic indicator library, the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data. The basic indicator library is constructed by domain experts from relevant documents in the rail transit field, summarizing rail transit-specific indicators and corresponding calculation logic. The rail transit-specific indicators include fault response time and at-fault rate. The relevant documents in the rail transit field include rail transit operation and maintenance logs, operation reports, and industry standard documents. The basic indicator library is dynamically updated at set intervals, and the MQL statement includes calculated indicators, involved dimensions, and calculation logic.
[0151] The SQL sample data acquisition module is used to acquire SQL sample data from the query-SQL data table in the ElasticSearch database and the Milvus vector database based on the user's query question.
[0152] The first SQL statement generation module is used to generate sample data, the MQL statement and the candidate data table based on the SQL, call the large model and generate the first SQL statement in combination with the prompt word template;
[0153] The data query module is used to perform data queries based on a thesaurus of synonyms with search conditions constructed by experts in the field of rail transit and the first SQL statement, and write the entire interactive process data into a MySQL database; each line of the thesaurus includes the current word, context, and mapped word.
[0154] This application constructs a basic indicator library containing rail transit-specific indicators and updates it in real time. It also optimizes the indicator matching logic by combining historical query examples, effectively making up for the deficiency of general NL2SQL in covering domain-specific indicators, and providing an accurate and comprehensive business foundation for subsequent semantic parsing.
[0155] This application establishes a fine-grained access control system tailored to rail transit positions, adapts to the strict "access-data" matching requirements in the rail transit field, solves the problem of "insufficient access management," and prevents unauthorized queries and leakage of sensitive information.
[0156] This application mines the relationships and connection patterns between tables from multiple sources in the field of rail transit, and adaptively adjusts the table selection strategy according to the number of tables to be queried, thereby improving the accuracy and efficiency of table selection and providing accurate data support for subsequent SQL generation.
[0157] This application is capable of undertaking the core responsibility of converting natural language queries into compliant SQL statements. Through the full-process control of "semantic parsing - structured indicators - compliance verification - intelligent reflection", it systematically improves the problem of low SQL generation quality and ensures SQL quality and business logic accuracy.
[0158] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0159] This document uses specific examples to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. Furthermore, those skilled in the art will recognize that, based on the ideas of this application, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A natural language query method for the rail transit field, characterized in that, include: Based on the ElasticSearch database and Milvus vector database, content matching and semantic matching are performed on the input user query to determine the first decomposition recall data and the second decomposition recall data. Based on the user permission dictionary, a candidate data table is constructed according to the first decomposed recall data and the second decomposed recall data; the user permission dictionary is an access permission dictionary for different roles constructed based on experts in the rail transit field; Based on the content-field data tables of the ElasticSearch database and the Milvus vector database, the first aligned recall data corresponding to the user query question is determined, and the entities in the user query question are extracted using a large model combined with prompt word templates. For each entity, semantic matching is performed on each entity based on the Milvus vector database to determine the second aligned recall data. Based on the description information of the candidate data table, the fields in the second aligned recall data are mapped to the same language as the user query and determined as the third aligned recall data. Based on the basic indicator library, a Metric Query Language (MQL) statement is constructed using the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data. The basic indicator library is constructed from rail transit-related documents, with rail transit-specific indicators and corresponding calculation logic manually summarized by domain experts. The rail transit-specific indicators include fault response time and at-fault rate. The rail transit-related documents include rail transit operation and maintenance logs, operation reports, and industry standard documents. The basic indicator library is dynamically updated at set intervals, and the MQL statement explicitly includes the calculated indicator, the involved dimensions, and the calculation logic. Based on the user query question, obtain SQL generation sample data from the query-SQL data table in the ElasticSearch database and the Milvus vector database; Based on the SQL, sample data, the MQL statement, and the candidate data table, the large model is invoked, and the first SQL statement is generated by combining the prompt word template. Data is queried based on a thesaurus of search criteria constructed by experts in the field of rail transit and the first SQL statement, and the data of the entire interaction process is written into the MySQL database. Each line of the thesaurus includes the current word, context, and the mapped word.
2. The natural language query method for the rail transit field according to claim 1, characterized in that, Based on the ElasticSearch and Milvus vector databases, content and semantic matching are performed on the input user query to determine the first and second decomposition recall data, specifically including: Based on the query decomposition data table of the ElasticSearch database, the content of the input user query is matched to determine the first decomposition of retrieved data. Based on the query decomposition data table of the Milvus vector database, semantic matching is performed on the input user query to determine the second decomposition recall data.
3. The natural language query method for the rail transit field according to claim 1, characterized in that, Based on the user permission dictionary, a candidate data table is constructed according to the first decomposed recall data and the second decomposed recall data, specifically including: Based on the rearrangement model, the first decomposed recall data and the second decomposed recall data are fused and re-ranked by relevance to determine the third decomposed recall data. The third decomposition and recall data is integrated into the configured prompt word template, and the large model is called to obtain the decomposition results and fine-grained query content of the user query. Based on the fine-grained query content and the access permission dictionary, determine whether the query content in the user's query question exceeds the range of data that the current user can query; If so, display the range of data that the current user can query, and show a prompt to re-enter the information; If not, select table data based on the user query question and construct a candidate data table.
4. The natural language query method for the rail transit field according to claim 3, characterized in that, Based on the basic indicator library, MQL statements are constructed according to the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data, specifically including: The frequency of use of each field in SQL queries within a historical period is statistically analyzed. Combined with the third alignment recall data, the large model and the prompt word template are used to filter search conditions and search fields, and the search results are used as the fourth alignment recall data. The fifth aligned recall data is determined by performing a reverse mapping based on the description information of the candidate data table and the field names of the fourth aligned recall data. Based on the basic indicator library, the fine-grained query content, and the fifth alignment recall data, a structured MQL statement is generated using a large model and in conjunction with the prompt word template.
5. The natural language query method for the rail transit field according to claim 4, characterized in that, Based on the SQL sample data, the MQL statement, and the candidate data table, the large model is invoked, and the first SQL statement is generated in conjunction with the prompt word template, specifically including: Based on the decomposition results of the user query, the DDL information of the candidate data table, the fifth alignment recall data, the MQL statement, and the SQL generation sample data, the large model is invoked, and the first SQL statement is generated in conjunction with the prompt word template.
6. The natural language query method for the rail transit field according to claim 5, characterized in that, Data querying is performed based on a thesaurus of synonyms constructed by experts in the field of rail transit and the first SQL statement, specifically including: The first SQL statement is input into the SQLGlot parser for syntax analysis and verification, and the verification result is determined; the verification includes checking the integrity of JOIN and verifying the legality of aggregate functions; If the verification result indicates an error was found, record the error type and location, provide error information, and update the error count. Determine whether the number of errors is less than the maximum number of attempts. If yes, return "Based on the decomposition results of the user query, the DDL information of the candidate data table, the fifth alignment recall data, the MQL statement, and the SQL generation sample data, call the large model and generate the first SQL statement in combination with the prompt word template". If no, set the query status to failure. If the verification result is no error found, the search conditions in the first SQL statement are replaced with synonyms based on the thesaurus, and a second SQL statement is generated. Execute the second SQL statement; If an error occurs during execution, record and report the error message, and update the error count; If the execution is successful, the query results will be returned to the user interface, and the query status will be set to success.
7. A natural language query system for the rail transit field, characterized in that, include: The content matching and semantic matching module is used to perform content matching and semantic matching on the input user query based on the ElasticSearch database and Milvus vector database to determine the first decomposition recall data and the second decomposition recall data. The candidate data table construction module is used to construct a candidate data table based on the user permission dictionary, according to the first decomposed recall data and the second decomposed recall data; the user permission dictionary is an access permission dictionary for different roles constructed by experts in the field of rail transit. The entity extraction module is used to determine the first aligned recall data corresponding to the user query question based on the content-field data table of the ElasticSearch database and the Milvus vector database, and to extract the entities in the user query question using a large model combined with prompt word templates. The mapping module is used to perform semantic matching on each entity based on the Milvus vector database to determine the second aligned recall data, and to map the fields in the second aligned recall data to the same language as the user query according to the description information of the candidate data table, and determine them as the third aligned recall data. The MQL statement construction module is used to construct Metric Query Language (MQL) statements based on a basic indicator library, the first decomposed recall data, the second decomposed recall data, the business indicator dictionary, and the third aligned recall data. The basic indicator library is constructed by domain experts from relevant documents in the rail transit field, summarizing rail transit-specific indicators and corresponding calculation logic. These rail transit-specific indicators include fault response time and at-fault rate. The relevant rail transit documents include rail transit operation and maintenance logs, operation reports, and industry standard documents. The basic indicator library is dynamically updated at set intervals, and the MQL statements explicitly include the calculated indicators, involved dimensions, and calculation logic. The SQL sample data acquisition module is used to acquire SQL sample data from the query-SQL data table in the ElasticSearch database and the Milvus vector database based on the user query question. The first SQL statement generation module is used to generate sample data, the MQL statement and the candidate data table based on the SQL, call the large model and generate the first SQL statement in combination with the prompt word template; The data query module is used to perform data queries based on a thesaurus of synonyms with search conditions constructed by experts in the field of rail transit and the first SQL statement, and write the data of the entire interaction process into the MySQL database. Each line of the thesaurus includes the current word, context, and the mapped word.
Citation Information
Patent Citations
SQL (Structured Query Language) generation method and system based on large language model, terminal and medium
CN119917526A
NL2SQL optimization method and device based on large model, equipment and medium
CN120743939A