Query statement construction method and system and data query method and system

By constructing query statements and using a syntax tree to generate query statements, the problems of error-prone construction and poor readability in the existing technology are solved, and the efficiency and accuracy of code development are improved.

CN120670535APending Publication Date: 2025-09-19CTRIP BUSINESS TRAVEL INFORMATION SERVICE (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510773395.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-11
Publication Date
2025-09-19

AI Technical Summary

Technical Problem

The existing query statement construction method is prone to errors and the code readability is poor, resulting in low development efficiency.

Method used

By obtaining the field name and conditions of the query target data, calling the corresponding query method to build a syntax tree, and generating a query statement based on the syntax tree, it avoids directly concatenating strings.

Benefits of technology

Improves code readability and maintainability, reduces runtime errors, and improves code development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120670535A_ABST
    Figure CN120670535A_ABST
Patent Text Reader

Abstract

The invention provides a query statement construction method and system and a data query method and system. The construction method comprises the following steps: acquiring a field name and a query condition required for querying target data; calling a plurality of corresponding first query methods based on the query conditions, and constructing a syntax tree based on the field names and the first query methods; and constructing a query statement for querying the target data according to the syntax tree. According to the method, the query statement is constructed through the syntax tree constructed through the field name and the first query method, the character strings are not directly spliced in the mode, and then running errors caused by character string splicing are avoided. Besides, in the process of constructing the query statements through the constructed syntax tree, a plurality of first query methods are called in a chained manner, so that the readability and maintainability of codes are higher. Therefore, the code development efficiency can be improved through the construction method.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a query statement construction method and system, and a data query method and system. Background Art

[0002] Elasticsearch (ES) is a distributed, scalable, near-real-time, high-performance search and data analytics engine built on Apache Lucene. A query string is a query method in ES. The query string syntax uses operators such as OR, AND, and NOT to parse and split the provided query string. Existing query statements based on ES query strings are constructed directly by concatenating strings. However, this construction method is not only prone to errors during the construction process, but also lacks readability in the code. This leads to low code development efficiency. Therefore, query construction methods still need to be improved. Summary of the Invention

[0003] The technical problem to be solved by the present disclosure is to overcome the defects of the query statement construction method in the prior art, which is not only prone to errors during the construction process but also has poor readability of the query statement construction code, and to provide a query statement construction method, system, data query method and system.

[0004] The present disclosure solves the above technical problems through the following technical solutions:

[0005] In a first aspect, a method for constructing a query statement is provided, the method comprising:

[0006] Get the field names and query conditions required to query the target data;

[0007] Calling corresponding first query methods based on the query condition;

[0008] Building a syntax tree based on the field name and the first query method;

[0009] A query statement for querying the target data is constructed according to the syntax tree.

[0010] Optionally, the first query method includes several second query methods and several third query methods; the second query method is used to filter data according to the query condition and the field name; the third query method is used to adjust the logical relationship and execution order of the second query method according to the query condition and the field name;

[0011] The step of constructing a syntax tree based on the field name and the first query method includes:

[0012] Constructing a syntax tree based on the field name, the second query method, and the third query method. Optionally, the third query method includes at least one of logical AND, logical NOT, and brackets;

[0013] and / or,

[0014] The construction method is applied to the creator mode in the full-text search engine.

[0015] Optionally, the step of constructing a query statement for querying the target data according to the syntax tree includes:

[0016] In response to an unrecognizable first query method appearing in the process of constructing the query statement according to the syntax tree, a preset escape rule is used to add an escape character to the unrecognizable first query method to obtain the query statement.

[0017] In a second aspect, a data query method is provided, the data query method comprising:

[0018] Target data is queried through a target query statement, wherein the target query statement is constructed according to the query statement construction method described above.

[0019] In a third aspect, a query statement construction system is provided, the construction system comprising:

[0020] The acquisition module is used to obtain the field names and query conditions required to query the target data;

[0021] A calling module calls corresponding first query methods based on the query conditions,

[0022] A first building module, constructing a syntax tree based on the field name and the first query method;

[0023] The second building module is used to build a query statement for querying the target data according to the syntax tree.

[0024] Optionally, the first query method includes several second query methods and several third query methods; the second query method is used to filter data according to the query condition and the field name; the third query method is used to adjust the logical relationship and execution order of the second query method according to the query condition and the field name;

[0025] The first building block includes:

[0026] A construction unit constructs a syntax tree based on the field name, the second query method, and the third query method.

[0027] Optionally, the third query method includes at least one of logical AND, logical NOT, and brackets;

[0028] and / or,

[0029] The construction method is applied to the creator mode in the full-text search engine.

[0030] Optionally, the second building block includes:

[0031] The response unit, in response to an unrecognizable first query method appearing in the process of constructing the query statement according to the syntax tree, uses a preset escape rule to add an escape character to the unrecognizable first query method to obtain the query statement.

[0032] In a fourth aspect, a data query system is provided, comprising:

[0033] The query module queries the target data through a target query statement, wherein the target query statement is constructed according to the query statement construction method mentioned above.

[0034] In a fifth aspect, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and for running on the processor, characterized in that when the processor executes the computer program, it implements the above-mentioned query statement construction method or the above-mentioned data query method.

[0035] In a sixth aspect, a computer-readable storage medium is provided, on which a computer program is stored, characterized in that when the computer program is executed by a processor, it implements the above-mentioned query statement construction method or the above-mentioned data query method.

[0036] In a seventh aspect, a computer program product is provided, comprising a computer program, characterized in that when the computer program is executed by a processor, it implements the above-mentioned query statement construction method or the above-mentioned data query method.

[0037] On the basis of conforming to the common sense in this field, the above-mentioned preferred conditions can be arbitrarily combined to obtain the preferred embodiments of the present disclosure.

[0038] The positive and progressive effect of this disclosure lies in: constructing a query statement using the syntax tree constructed using the field name and the first query method. This method of constructing the query statement does not directly concatenate strings, thereby avoiding runtime errors that may be caused by string concatenation. In addition, when constructing the query statement using the constructed syntax tree, several first query methods are chained, thereby enhancing the readability and maintainability of the code. Therefore, this query statement construction method can improve the efficiency of code development. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 A flowchart of a method for constructing a query statement provided in Example 1 of the present disclosure;

[0040] Figure 2 A code example diagram of a syntax tree in a query statement construction method provided in Example 1 of the present disclosure;

[0041] Figure 3 A schematic diagram of modules of a query statement construction system provided in Example 3 of the present disclosure;

[0042] Figure 4 A schematic diagram of a data query system according to Embodiment 4 of the present disclosure;

[0043] Figure 5 This is a structural diagram of an electronic device shown in Example 5 of the present disclosure. DETAILED DESCRIPTION

[0044] The present disclosure is further illustrated below by way of examples, but the present disclosure is not limited to the scope of the examples.

[0045] In the embodiments of the present disclosure, prefixes such as "first" and "second" are used only to distinguish different description objects and have no limiting effect on the position, order, priority, quantity or content of the described objects. In the embodiments of the present disclosure, the use of prefixes such as ordinal numbers to distinguish description objects does not constitute a restriction on the described objects. For the statement of the described objects, please refer to the description in the context of the embodiments, and the use of such prefixes should not constitute an unnecessary restriction. In addition, in the description of this embodiment, unless otherwise specified, the meaning of "plurality" is two or more.

[0046] Example 1

[0047] In order to improve the efficiency of code development, the present disclosure provides a method for constructing a query statement in Embodiment 1. Preferably, the method for constructing a query statement is applied to business travel account services. Figure 1 This is a flowchart of a method for constructing a query statement provided in Example 1 of the present disclosure. The method for constructing a query statement includes the following steps:

[0048] Step 101: Obtain the field names and query conditions required for querying target data.

[0049] For example, a user wants to query a company ID (Identity document) in List A, the company type is a or b, and the company's region is not China.

[0050] Therefore, if the user wants to find the above data, he or she must not only know the field names of the Enterprise ID, List A, Company Type, Company Region, and China, but also the query conditions such as "Enterprise ID is in List A", "Company Type is A or B", and "Company Region is not a Chinese Company".

[0051] Step 102: Call corresponding first query methods based on the query conditions.

[0052] For example, if you want to query data where the company ID is in list A, the company type is a or b, and the company's region is not China:

[0053] Then, the user needs to call several corresponding first query methods based on the query conditions of "Company ID is in List A", "Company Type is A or B", and "Company's Region is not China".

[0054] For example, if the query conditions are in an and / or relationship, you need to call the first query method .and() / .or() to perform concatenation.

[0055] For example, if a query contains nested conditions ("Company type is a or b" contains two other conditions nested within it), the first query method .leftBracket() and .rightBracket() are needed to treat "Company type is a or b" as a logical unit. .leftBracket() is the left bracket, and .rightBracket() is the right bracket.

[0056] It is also necessary to call some first query methods to filter data according to the query conditions, for example, calling the .exactIn() method to filter out the data in list A whose company ID is.

[0057] In addition, it should be noted that before calling the first query method in the Builder mode in Elasticsearch, it is also necessary to call the dependency declaration of the Maven project.

[0058] The code implementation can be:

[0059]

[0060] Step 103: Build a syntax tree based on the field name and the first query method.

[0061] For example, if a user wants to query data where the company ID is in list A, the company type is a or b, and the company's region is not China: then the syntax tree constructed based on the field name and the first query method is as follows: Figure 2As shown: Among them, the field name of the enterprise ID is corp_corporation, the field name of the A list is corpIds, the field name of the company type is accountType, the field name of the company region is POS, and the field name of China is ZH-CN.

[0062] .exactIn(), .and(), .leftBracket(), .match(), .or(), .rightBracket(), .exist s(), .not(), and .exactMatch() are all first-query methods.

[0063] Using the company ID field name corpIds, the field name of list A corp_corporation, and the first query method .exactIn() method, we get

[0064] .exactIn("corp_corporation",corpIds) can be used to filter out data whose corpIds is in the corp_corporation list;

[0065] Using the company type field name accountType and the first query method .match() to obtain .match("accountType","a") can be used to filter out data with accountType a;

[0066] Using the company type field name accountType and the first query method .match() to obtain .match("accountType","b") can be used to filter out data with accountType b;

[0067] Use the field name of the company's region as POS and the first query method .exists() to get .exists("P0S"), which can be used to filter out data that exists for POS;

[0068] Using the field name of the company's region as POS, the field name of China as ZH-CN, and .exactMatch()'s .exactMatch("P0S",PosEnum.ZH-CN.getCode()) can be used to filter out data where POS exists and POS is ZH-CN.

[0069] Use .and(), .leftBracket(), .or(), and .not() to concatenate the above field names and the partial query statement obtained by the first query method to obtain a syntax tree.

[0070] Step 104: Construct a query statement for querying the target data according to the syntax tree.

[0071] For example, you can Figure 2 String queryString = esSearchBuilder.build() in the query string constructs the above syntax tree into a query statement for searching the target data. Specifically, the query statement is a query_string statement.

[0072] The query statement constructed is: (corp_corporation:corpIds)AND(accountType:a ORaccountType ORb)AND(_exists_:POS AND NOT POS:ZH-CN).

[0073] It should be noted that the query statement construction process can be implemented in the esBuilder tool built by relevant personnel.

[0074] In this embodiment, a query statement is constructed using the field name and the syntax tree constructed using the first query method. This query construction method avoids direct string concatenation, thereby avoiding runtime errors that may result from string concatenation. Furthermore, during the process of constructing the query statement using the constructed syntax tree, several first query methods are chained, making the code more readable and maintainable. This query construction method can thus improve code development efficiency.

[0075] In one embodiment, the first query method includes several second query methods and several third query methods; the second query method is used to filter data according to the query conditions and field names; the third query method is used to adjust the logical relationship and execution order of the second query method according to the query conditions and field names.

[0076] The step of constructing a syntax tree based on the field name and the first query method includes: constructing a syntax tree based on the field name, the second query method and the third query method.

[0077] For example, the second query method can be Figure 2 At least one of .exactIn(), .match(), .exists(), and .exactMatch() is used to filter data based on query conditions and field names. The third query method can be Figure 2 The .and(), .leftBracket(), .or(), and .not() methods are used to adjust the logical relationship and execution order of the second query method.

[0078] In this embodiment, constructing a syntax tree based on the field name, the second query method and the third query method can make the execution order of the syntax tree and the logical relationship between each query method more reasonable, thereby making the subsequently constructed query statement more accurate.

[0079] In one embodiment, the third query method includes at least one of logical AND, logical NOT, and brackets.

[0080] In this embodiment, by calling a third query method including one of logical AND, logical NOT, and brackets, the execution order of the constructed syntax tree and the logical relationship between each query method can be made more reasonable, thereby making the query statement constructed subsequently more accurate.

[0081] In one embodiment, the construction method is applied to the creator mode in the full-text search engine.

[0082] In this embodiment, since the creator mode (for example, Builder mode) in the full-text search engine (for example, full-text search engine) can verify the called first query method according to its own grammatical rules during the statement construction process, compared with the method of manually concatenating character strings to construct query statements, the method provided by the present disclosure can also pre-verify the constructed query statements, thereby making the constructed query statements more accurate.

[0083] In one embodiment, the step of constructing a query statement for querying target data according to the syntax tree includes:

[0084] In response to an unrecognizable first query method appearing in the process of constructing a query statement according to the syntax tree, a preset escape rule is used to add an escape character to the unrecognizable first query method to obtain a query statement.

[0085] For example, when the first query method is represented by symbols such as "+", "-", "=", "&&", "||", "!", "(", "")", "{", "}", "[", "]", "^", "\", "~", "*", "?", ":", "\\", " / ", "", and "\t", then the query statement may not be recognized in the process of constructing the syntax tree.

[0086] Then you need to add the escape character \\ according to the escape rules to convert the above symbols into "\\+", "\\-", "\\=", "\\&&", "\\||", "\\!", "\\(","\\)", "\\{", "\\}", "\\[", "\\]", "\\^", "\\\"", "\\~", "\\*", "\\?", "\\:", "\\\\", "\\ / ", "\\", and \\t so that these symbols can be correctly recognized.

[0087] In this embodiment, by adding an escape character to the unrecognizable first query method according to the preset escape rule, not only can the error of manually adding the escape character be avoided, but also the debugging and maintenance costs of relevant personnel can be reduced, thereby improving the code development efficiency.

[0088] Example 2

[0089] In order to improve the accuracy of data query, embodiment 2 of the present disclosure provides a data query method. This method is applied in the Builder mode in Elasticsearch. In addition, preferably, this data query method is applied to business travel account business. The query method includes:

[0090] The target data is queried through a target query statement, wherein the target query statement is constructed using the query statement construction method described above.

[0091] In this embodiment, the query statement constructed by the above query statement construction method can more accurately query the target data.

[0092] Example 3

[0093] Corresponding to the aforementioned query statement construction method embodiment, the present disclosure also provides a query statement construction system. Figure 3 A schematic diagram of a module of a query statement construction system provided in Example 3 of the present disclosure; the construction system 30 includes:

[0094] The acquisition module 31 is used to obtain the field name and query conditions required for querying the target data;

[0095] The calling module 32 calls corresponding first query methods based on the query conditions.

[0096] A first building module 33 builds a syntax tree based on the field name and the first query method;

[0097] The second building module 34 is configured to build a query statement for querying the target data according to the syntax tree.

[0098] In one embodiment, the first query method includes several second query methods and several third query methods; the second query method is used to filter data according to the query conditions and the field names; the third query method is used to adjust the logical relationship and execution order of the second query method according to the query conditions and the field names; the first construction module 43 includes:

[0099] A construction unit constructs a syntax tree based on the field name, the second query method, and the third query method.

[0100] In one embodiment, the third query method includes at least one of logical AND, logical NOT, and brackets;

[0101] and / or,

[0102] The construction method is applied to the creator mode in the full-text search engine.

[0103] In one embodiment, the second building block 34 includes:

[0104] The response unit, in response to an unrecognizable first query method appearing in the process of constructing the query statement according to the syntax tree, uses a preset escape rule to add an escape character to the unrecognizable first query method to obtain the query statement.

[0105] Since the system embodiments generally correspond to the method embodiments, reference will be made to the description of the method embodiments for relevant details. The system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separate, and the components of the units may or may not be physical units, i.e., they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the disclosed solution.

[0106] Example 4

[0107] Corresponding to the aforementioned data query method embodiment, the present disclosure also provides a data query system embodiment. The data query system 40 includes:

[0108] The query module 41 queries the target data through a target query statement, wherein the target query statement is constructed according to the query statement construction method mentioned above.

[0109] Since the system embodiments generally correspond to the method embodiments, reference will be made to the description of the method embodiments for relevant details. The system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separate, and the components of the units may or may not be physical units, i.e., they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the disclosed solution.

[0110] Example 5

[0111] Figure 5 This is a structural diagram of an electronic device showing an example embodiment of the present disclosure, wherein the electronic device includes a memory, a processor, and a computer program stored in the memory and for running on the processor, and when the processor executes the computer program, it implements the query statement construction method described in any of the above embodiments or the data query method described above. Figure 5 The electronic device 50 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0112] like Figure 5 As shown, the electronic device 50 may be a general-purpose computing device, such as a server device. Components of the electronic device 50 may include, but are not limited to, the at least one processor 51, the at least one memory 52, and a bus 53 connecting different system components (including the memory 52 and the processor 51).

[0113] The bus 53 includes a data bus, an address bus, and a control bus.

[0114] The memory 52 may include a volatile memory, such as a random access memory (RAM) 521 and / or a cache memory 522 , and may further include a read-only memory (ROM) 523 .

[0115] The memory 52 may also include a program tool 525 (or utility) having a set (at least one) of program modules 524, such program modules 524 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.

[0116] The processor 51 executes various functional applications and data processing by running the computer program stored in the memory 52, such as the query statement construction method provided in any of the above embodiments or the data query method described above.

[0117] The electronic device 50 can also communicate with one or more external devices 54 (e.g., a keyboard, pointing device, etc.). Such communication can be performed via an input / output (I / O) interface 55. Furthermore, the electronic device 50 can also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 56. As shown, the network adapter 56 communicates with other modules of the electronic device 50 via a bus 53. It should be understood that, although not shown in the figure, other hardware and / or software modules can be used in conjunction with the electronic device 50, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID (RAID) systems, tape drives, and data backup storage systems.

[0118] It should be noted that although several units / modules or sub-units / modules of the electronic device are mentioned in the detailed description above, this division is merely exemplary and not mandatory. In fact, according to the embodiments of the present disclosure, the features and functions of two or more units / modules described above can be embodied in one unit / module. Conversely, the features and functions of one unit / module described above can be further divided and embodied by multiple units / modules.

[0119] Example 6

[0120] An embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the query statement construction method provided in any of the above embodiments or the data query method described above.

[0121] The readable storage medium may include, but is not limited to, a portable disk, a hard disk, a random access memory, a read-only memory, an erasable programmable read-only memory, an optical storage device, a magnetic storage device, or any suitable combination thereof.

[0122] Example 7

[0123] An embodiment of the present disclosure further provides a computer program product, including a computer program, which, when executed by a processor, implements any of the above-mentioned query statement construction methods or the above-mentioned data query methods.

[0124] The program code for executing the computer program product of the present disclosure may be written in any combination of one or more programming languages, and the program code may be executed entirely on the user device, partially on the user device, as a standalone software package, partially on the user device and partially on a remote device, or entirely on the remote device.

[0125] While specific embodiments of the present disclosure have been described above, those skilled in the art will appreciate that these are merely illustrative and that the scope of protection of the present disclosure is defined by the appended claims. Those skilled in the art may make various changes or modifications to these embodiments without departing from the principles and essence of the present disclosure, and such changes and modifications are intended to fall within the scope of protection of the present disclosure.

Claims

1. A method for constructing a query statement, characterized in that: The construction method comprises: Get the field names and query conditions required to query the target data; Calling corresponding first query methods based on the query condition; Building a syntax tree based on the field name and the first query method; A query statement for querying the target data is constructed according to the syntax tree.

2. The construction method according to claim 1, wherein The first query method includes several second query methods and several third query methods; the second query method is used to filter data according to the query condition and the field name; The third query method is used to adjust the logical relationship and execution order of the second query method according to the query condition and the field name; The step of constructing a syntax tree based on the field name and the first query method includes: A syntax tree is constructed based on the field name, the second query method, and the third query method.

3. The construction method according to claim 2, wherein: The third query method includes at least one of logical AND, logical NOT, and brackets; and / or, The construction method is applied to the creator mode in the full-text search engine.

4. The construction method according to any one of claims 1 to 3, wherein: The step of constructing a query statement for querying the target data according to the syntax tree includes: In response to an unrecognizable first query method appearing in the process of constructing the query statement according to the syntax tree, a preset escape rule is used to add an escape character to the unrecognizable first query method to obtain the query statement.

5. A data query method, characterized in that: The data query method includes: The target data is queried through a target query statement, wherein the target query statement is constructed according to the query statement construction method according to any one of claims 1 to 4.

6. A query statement construction system, characterized in that: The build system includes: The acquisition module is used to obtain the field names and query conditions required to query the target data; A calling module calls corresponding first query methods based on the query conditions, A first building module, constructing a syntax tree based on the field name and the first query method; The second building module is used to build a query statement for querying the target data according to the syntax tree.

7. A data query system, characterized in that: The data query system includes: A query module queries target data through a target query statement, wherein the target query statement is constructed according to the query statement construction method according to any one of claims 1 to 4.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and configured to run on the processor, wherein: When the processor executes the computer program, the query statement construction method described in any one of claims 1 to 4 or the data query method described in claim 5 is implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the query statement construction method described in any one of claims 1 to 4 or the data query method described in claim 5 is implemented.

10. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the query statement construction method described in any one of claims 1 to 4 or the data query method described in claim 5 is implemented.