Multi-condition query method based on ElasticSearch
By converting user-defined multi-condition query conditions into query statements that comply with the ElasticSearch engine, and using recursive logic decision function processing, the problem of low multi-condition search efficiency in ElasticSearch is solved and efficient query processing is achieved.
Patent Information
- Application Number
- CN202410645870.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-23
- Publication Date
- 2024-09-06
AI Technical Summary
In the existing technology, the efficiency of multi-condition search using ElasticSearch is low. Users need to frequently modify query conditions and logical relationships to adapt to complex query requirements, resulting in low development efficiency.
Provides a multi-condition query method based on ElasticSearch. By receiving user-defined query conditions, converting them into query statements in json format, and using recursive logic decision functions to parse and build query condition trees, it supports queries of multi-layer logical nested relationships. Directly enter the ElasticSearch search engine to query.
Multiple logical combination queries of any conditions can be implemented without modifying the code, which improves query efficiency and simplifies the processing of complex query conditions.
Smart Images

Figure CN118606340A
Abstract
Description
Technical Field
[0001] The embodiments of this specification relate to the field of computer technology, and in particular to a multi-condition query method based on ElasticSearch. Background Art
[0002] ElasticSearch (ES for short) is an open source, distributed, scalable, real-time data search and analysis engine based on Apache Lucene. It uses the "document collection" data model to organize data and provides real-time search and analysis capabilities. ES can be regarded as a distributed document database, and each field is indexed data and can be searched. When the user enters a field query, it will search according to the pre-established index and return the found results. ES can provide users with fixed query templates. Users can only perform specific queries through existing multi-condition query templates. However, in daily development, some similar queries may need to be used frequently. In order to reduce repetitive work, these similar queries can be written as templates. In the future, you only need to pass different parameters to call the template to get the results.
[0003] In ES, the use of combined condition queries is a powerful feature of it as a search engine to retrieve data. The query syntax of ES provides basic add, delete, modify and query functions. The syntax of its combined conditions is called bool. By splicing ES-specific syntax in bool, multi-condition splicing queries can be performed in most scenarios. However, this spliced query template cannot well meet the scenarios where users want to customize the assembled query conditions. When it is necessary to add a few conditions to the original query conditions or change the original conditions, or when there are many and complex query requirements, it is necessary to rewrite the query system based on the query conditions and the logical relationship between the conditions to adapt to the changes in requirements. This results in a corresponding ES javaapi query method for each requirement, which leads to the problem of low efficiency for multi-condition queries. Therefore, there is an urgent need for a multi-condition query method that can solve the problem of low efficiency of multi-condition search using ES in the prior art. Summary of the invention
[0004] In view of the above problems in the prior art, the purpose of the embodiments of this specification is to provide a multi-condition query method based on ElasticSearch to solve the problem of low efficiency of multi-condition search using ES in the prior art.
[0005] In order to solve the above technical problems, the specific technical solutions of the embodiments of this specification are as follows:
[0006] On the one hand, an embodiment of this specification provides a multi-condition query method based on ElasticSearch, the method comprising:
[0007] Receive user-defined query conditions;
[0008] Convert the custom query condition into a query statement in json format, wherein the query statement contains multiple layers of logical nested relationships;
[0009] Parse the query statement from outside to inside according to the agreed fields to obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions;
[0010] Recursively calling a predefined logic determination function so that the logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements to obtain a search query matching pattern of each layer of query statements;
[0011] The search query matching pattern and corresponding sub-conditions of each layer of query statements are input into the ElasticSearch search engine for query, and the query results are returned to the user.
[0012] Furthermore, converting the custom query condition into a query statement in JSON format includes:
[0013] Parsing the user-defined query condition, and converting the user-defined query condition into a predefined data structure according to the parsed content, wherein the data structure includes a field name, an operator, a field value, and a logical identifier;
[0014] Constructing a query condition tree according to the data structure;
[0015] The query condition tree is traversed, and the query condition tree is converted into a json statement using a json module.
[0016] Further, the logic identifier includes an AND identifier and an OR identifier, and the search query matching mode of each layer of query statements includes:
[0017] If the logic identifier is AND, the search query matching mode of each layer of query statements includes a full field matching mode, a partial field matching mode, and a time range matching mode;
[0018] If the logic identifier is OR, the search query matching mode of each layer of query statements is any one of a full field matching mode, a partial field matching mode, or a time range matching mode.
[0019] Furthermore, before parsing the query statement from the outside to the inside according to the agreed fields, the method further includes:
[0020] Defining a query object structure, wherein the query object structure includes an object type and an object attribute;
[0021] Extract key information from the query conditions;
[0022] Extracting semantic information from the key information using a semantic information extraction model to obtain a semantic representation;
[0023] Mapping the semantic representation to the query object structure to obtain mapping information;
[0024] A query object is constructed according to the mapping information.
[0025] Furthermore, the agreed fields include an AND field and an OR field, and the query statement is parsed from the outside to the inside according to the agreed fields to obtain a logical identifier of each layer of the query statement and a corresponding sub-condition, including:
[0026] Determining the logical structure of the query statement;
[0027] Parsing starts from the outermost query statement, determining the agreed fields contained in the outermost query statement as logical identifiers, and obtaining sub-conditions contained in the outermost query statement;
[0028] Determine whether the sub-conditions included in the outermost query statement include an agreed field, and if so, determine the corresponding agreed field as a logical identifier of the second outer query statement;
[0029] Repeat the above steps until the sub-conditions contained in the query statement at the current level do not contain the agreed field, and obtain the logical identifier of the query statement at each level and the corresponding sub-conditions.
[0030] Furthermore, the recursive calling of a predefined logic determination function so that the logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements includes:
[0031] Determine whether the logical identifier in each layer of query statements is empty, and if so, exit the recursion;
[0032] If not, the search query matching mode of the query statement is determined according to the logical identifier.
[0033] On the other hand, an embodiment of the present specification provides a multi-condition query device based on ElasticSearch, the device comprising:
[0034] A receiving module, used for receiving user-defined query conditions;
[0035] A conversion module, used to convert the custom query condition into a query statement in JSON format, wherein the query statement contains multiple layers of logical nested relationships;
[0036] A first parsing module, used to parse the query statement from the outside to the inside according to the agreed fields, and obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions;
[0037] A second parsing module is used to recursively call a predefined logic determination function, so that the logic determination function performs a logic determination on each layer of query statements according to the logic identifier of each layer of query statements, and obtains a search query matching pattern of each layer of query statements;
[0038] The query module is used to input the search query matching pattern and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and return the query results to the user.
[0039] On the other hand, an embodiment of the present specification further provides a computer device, including a memory, a processor, and a computer program stored in the memory, wherein when the computer program is executed by the processor, the instructions of any one of the above methods are executed.
[0040] On the other hand, an embodiment of the present specification further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor of a computer device, the computer program executes instructions of any one of the above-described methods.
[0041] On the other hand, the embodiments of this specification further provide a computer program product, which, when executed by a processor of a computer device, executes instructions of any one of the above methods.
[0042] One or more technical solutions provided by some embodiments of this specification have at least the following technical effects:
[0043] This manual first converts the user-defined query conditions containing multiple layers of logical nested relationships into query statements in json format, and then parses the query statements from the outside to the inside according to the agreed fields to obtain the logical identifier of each layer of query statements and the corresponding sub-conditions, and then recursively calls the pre-defined logical judgment function so that the logical judgment function performs logical judgment on each layer of query statements according to the logical identifier of each layer of query statements to obtain the search query matching mode of each layer of query statements, and finally inputs the search query matching mode and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and returns the query results to the user. In this way, complex query conditions containing multiple layers of logical nested relationships can be converted into conditions that conform to the ElasticSearch search engine, and any condition and multiple logical combination queries can be implemented without modifying the code, with high query efficiency.
[0044] The above description is only an overview of the technical solutions of some embodiments of this specification. In order to more clearly understand the technical means of some embodiments of this specification, they can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the embodiments of this specification more obvious and easy to understand, the following specifically cites the preferred embodiments and describes them in detail in conjunction with the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0046] Figure 1 A schematic diagram of a multi-condition query method based on ElasticSearch in some embodiments of this specification is shown;
[0047] Figure 2 A schematic diagram showing a process of converting a custom query condition into a query statement in JSON format in some embodiments of this specification;
[0048] Figure 3 A schematic diagram of a process of converting a user-defined query condition into a query object in some embodiments of this specification is shown;
[0049] Figure 4 A schematic diagram of a process of parsing the query statement from the outside to the inside according to the agreed fields in some embodiments of this specification to obtain the logical identification of each layer of the query statement and the corresponding sub-conditions;
[0050] Figure 5 A schematic diagram of a process in which a logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements in some embodiments of this specification is shown;
[0051] Figure 6 A schematic diagram of the module structure of a multi-condition query device based on ElasticSearch in some embodiments of this specification is shown;
[0052] Figure 7 A schematic diagram of the structure of a computer device in this specification is shown.
[0053] Description of the accompanying symbols:
[0054] 601, receiving module;
[0055] 602, conversion module;
[0056] 603, first parsing module;
[0057] 604, second parsing module;
[0058] 605. Query module;
[0059] 702. Computer equipment;
[0060] 704, processor;
[0061] 706. Memory;
[0062] 708, driving mechanism;
[0063] 710, input / output module;
[0064] 712. Input devices;
[0065] 714. Output devices;
[0066] 716. Presentation equipment;
[0067] 718. Graphical user interface;
[0068] 720, network interface;
[0069] 722. Communication link;
[0070] 724. Communication bus. DETAILED DESCRIPTION
[0071] The following will be combined with the drawings in the embodiments of this specification to clearly and completely describe the technical solutions in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this specification.
[0072] In order to solve the above problems, the embodiments of this specification provide a multi-condition query method based on ElasticSearch. Figure 1 It is a flowchart of a multi-condition query method based on ElasticSearch provided in the embodiment of this specification. This specification provides method operation steps as described in the embodiment or flowchart, but may include more or fewer operation steps based on conventional or non-creative labor. The order of steps listed in the embodiment is only one way of executing the order of many steps and does not represent the only execution order. When the system or device product is executed in practice, it can be executed in the order of the method shown in the embodiment or the accompanying drawings or in parallel.
[0073] It should be noted that the terms "first", "second", etc. in this specification and claims and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of this specification described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, device, product or equipment that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0074] Reference Figure 1 As shown, the embodiment of this specification provides a multi-condition query method based on ElasticSearch, and the method includes:
[0075] S101: receiving user-defined query conditions;
[0076] S102: converting the custom query condition into a query statement in json format, wherein the query statement contains multiple layers of logical nested relationships;
[0077] S103: parsing the query statement from outside to inside according to the agreed fields to obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions;
[0078] S104: recursively calling a predefined logic determination function, so that the logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements, and obtains a search query matching pattern of each layer of query statements;
[0079] S105: Input the search query matching pattern and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and return the query results to the user.
[0080] This manual first converts the user-defined query conditions containing multiple layers of logical nested relationships into query statements in json format, and then parses the query statements from the outside to the inside according to the agreed fields to obtain the logical identifier of each layer of query statements and the corresponding sub-conditions, and then recursively calls the pre-defined logical judgment function so that the logical judgment function performs logical judgment on each layer of query statements according to the logical identifier of each layer of query statements to obtain the search query matching mode of each layer of query statements, and finally inputs the search query matching mode and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and returns the query results to the user. In this way, complex query conditions containing multiple layers of logical nested relationships can be converted into conditions that conform to the ElasticSearch search engine, and any condition and multiple logical combination queries can be implemented without modifying the code, with high query efficiency.
[0081] In some embodiments, user-defined query conditions may include basic fields, date and time ranges, numerical ranges, words that represent logical relationships, and custom fields, etc. Among them, basic fields may be basic personal information or data identifiers such as name and mobile phone number, date and time ranges may be specific months, years, or a specified time period, such as querying sales data within a certain month or time period, numerical ranges may be specified numerical upper and lower limits, such as querying orders with sales within a certain interval, words that represent logical relationships include "and", "and", "or", and "or", etc., such as when a user queries data that meets condition A and condition B, and custom fields refer to fields that are customized by the user according to needs, such as file content, file category, etc., in addition to the default fields provided by the system.
[0082] Since the user-defined query conditions are semantic conditions, it is necessary to convert the semantic conditions into a computer-recognizable format. In order to be compatible with the statement format supported by ES, in some embodiments, refer to Figure 2 , the step of converting the custom query condition into a query statement in json format includes:
[0083] S201: Parse the user-defined query condition, and convert the user-defined query condition into a predefined data structure according to the parsed content, wherein the data structure includes a field name, an operator, a field value, and a logical identifier.
[0084] To parse the custom query conditions, you can first split the input string into the condition part and the operator, then split the condition part into the field name and field value according to the operator, and finally remove the blank characters on both sides of the field name and field value, and add type conversion logic as needed to obtain the parsed content, and convert the parsed content into a predefined data structure. For example, if the custom query condition is: query employees whose department is sales, or whose salary is greater than 5000 and whose position is manager, then parse the condition to obtain field names: "department", "salary", "position", field values: "sales", "5000", "manager", operators: "=", ">", "=", logical identifiers: "OR", "AND".
[0085] S202: Construct a query condition tree according to the data structure.
[0086] The query condition tree reflects the logical nested relationship of the query conditions. Each node in the query condition tree can be a triple of field name-operator-field value, or a logical operator node containing child nodes.
[0087] S203: traverse the query condition tree, and convert the query condition tree into a json statement using a json module.
[0088] The json module traverses the query condition tree by calling its built-in serialization function to convert the query condition tree into the corresponding json statement. For example, a custom query condition "Meet conditions 1, 2, and 3 at the same time or meet conditions 4 and 5 at the same time and meet conditions 6 and 7 at the same time" can be converted into the following json format:
[0089]
[0090] In some embodiments, the logic identifier includes an AND identifier and an OR identifier, and the search query matching pattern of each layer of query statements includes:
[0091] If the logic identifier is AND, the search query matching mode of each layer of query statements includes a full field matching mode, a partial field matching mode, and a time range matching mode;
[0092] If the logic identifier is OR, the search query matching mode of each layer of query statements is any one of a full field matching mode, a partial field matching mode, or a time range matching mode.
[0093] The all-fields matching mode means that the query condition fully matches all the relevant fields of a record in the database or data set. Matching records are returned only when each field in the query condition fully matches the corresponding field in the target record. The partial-fields matching mode allows the query condition to match only some fields of the target record. As long as some fields in the query condition match the corresponding fields in the target record, matching records can be returned. The time range matching mode means that based on the specified start and end time, the records that meet the conditions within the time range are searched.
[0094] In some embodiments, reference Figure 3 , before parsing the query statement from outside to inside according to the agreed fields, it also includes:
[0095] S301: define a query object structure, wherein the query object structure includes an object type and an object attribute;
[0096] S302: extracting key information from the query condition;
[0097] S303: extracting semantic information from the key information using a semantic information extraction model to obtain a semantic representation;
[0098] S304: Map the semantic representation to the query object structure to obtain mapping information;
[0099] S305: Construct a query object according to the mapping information.
[0100] Both AND and OR are logical relationships. This logical identifier is abstracted through Java objects and turned into an andOrQuery object. When a user-defined query condition is passed in, the AND or OR in the condition is reassigned to the andOrQuery object in sequence. By creating a query object, multiple conditions of OR logic can be assembled through the QueryBuilders.multiMatchQuery() function, while the conditions of AND logic can be directly encapsulated through the must() function.
[0101] In some embodiments, reference Figure 4 , the agreed fields include an AND field and an OR field, and the query statement is parsed from the outside to the inside according to the agreed fields to obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions, including:
[0102] S401: Determine the logical structure of the query statement;
[0103] S402: parsing starts from the outermost query statement, determining the agreed fields included in the outermost query statement as logical identifiers, and obtaining sub-conditions included in the outermost query statement;
[0104] S403: Determine whether the sub-conditions included in the outermost query statement include an agreed field, and if so, determine the corresponding agreed field as a logical identifier of the second outer query statement;
[0105] S404: Repeat the above steps until the sub-conditions included in the query statement at the current level do not contain the agreed field, and obtain the logical identifier of the query statement at each level and the corresponding sub-conditions.
[0106] The logical structure of the query statement can be determined according to the brackets and indents in the json statement of the custom query condition, and the logical structure reflects the multi-layer logical nesting relationship of the query statement. Taking the above json statement as an example, parsing starts from the outermost layer, which contains the logical identifier "AND". According to the brackets and indents in the json statement, it can be determined that there are two sub-conditions corresponding to the logical identifier. For each sub-condition, it is determined in turn whether it contains the logical identifier. For example, in the first sub-condition, parsing starts from its outermost layer, it can be known that its outermost layer contains the logical identifier "OR", and it is obtained that it contains two sub-conditions. For the second sub-condition, the outermost layer is parsed to contain the logical identifier "AND". Next, it is determined whether the sub-conditions contained in the second sub-condition contain the logical identifier. From the json statement, it can be seen that it does not contain the logical identifier. Thus, the parsing from the outside to the inside is completed, and the sub-conditions and corresponding logical identifiers contained in each layer of the query statement are obtained.
[0107] In some embodiments, reference Figure 5 , the recursive calling of a predefined logic determination function so that the logic determination function performs a logic determination on each layer of query statements according to the logic identifier of each layer of query statements, including:
[0108] S501: Determine whether the logical identifier in each layer of the query statement is empty, and if so, exit the recursion;
[0109] S502: If not, determine the search query matching mode of the query statement according to the logical identifier.
[0110] In some embodiments, the logic judgment function can be defined as createQuery(). The createQuery() judgment function has two key judgment steps. The first step is to determine whether the passed andOrQuery object is empty. If it is empty, it means that the query statement does not contain a logical identifier, and the recursion is jumped out. If it is not empty, it will read whether the current round of recursion is an AND identifier or an OR identifier, and enter the AND or OR logic according to the identifier. As shown in the following simple json, the user wants to query to meet any one of condition 1, condition 2, and condition 3. The logical relationship is an OR relationship, so the logical identifier is OR.
[0111] {"OR":[{condition 1},{condition 2},{condition 3}]}
[0112] After receiving the above json, it will first parse the logical identifier OR, and then call the recursive function. The recursive function determines that conditions 1, 2, and 3 have no nested logic, and will not perform the next layer of recursive loop. Therefore, only one layer of judgment is performed, and then the ES condition search can be performed directly. If the json is multi-layer nested, the recursive function will be called multiple times according to the AND and OR signs until there is no logical identifier in the inner layer.
[0113] This manual can convert user-defined query conditions into query conditions that conform to the ES engine, support open access, and implement multiple logical combination queries of any conditions without modifying the code, facilitate multiple system access to achieve full-text search queries, and improve query efficiency.
[0114] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. And the acquisition, storage, use, and processing of data in the technical solutions described in the embodiments of this application are in compliance with relevant regulations.
[0115] Based on the above-mentioned multi-condition query method based on ElasticSearch, the embodiment of this specification also provides a multi-condition query device based on ElasticSearch. The device may include a system (including a distributed system), software (application), module, component, server, client, etc. using the method described in the embodiment of this specification and a device combined with necessary implementation hardware. Based on the same innovative concept, the device in one or more embodiments provided in the embodiment of this specification is as described in the following embodiments. Since the implementation scheme and method of the device to solve the problem are similar, the implementation of the specific device in the embodiment of this specification can refer to the implementation of the aforementioned method, and the repetitions will not be repeated. As used below, the term "unit" or "module" can implement a combination of software and / or hardware for predetermined functions. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceived.
[0116] Specifically, Figure 6 This is a schematic diagram of the module structure of an embodiment of a multi-condition query device based on ElasticSearch provided in the embodiment of this specification, referring to Figure 6 As shown, a multi-condition query device based on ElasticSearch provided in an embodiment of this specification includes:
[0117] Receiving module 601, used for receiving user-defined query conditions;
[0118] A conversion module 602, configured to convert the user-defined query condition into a query statement in JSON format, wherein the query statement contains multiple layers of logical nested relationships;
[0119] The first parsing module 603 is used to parse the query statement from the outside to the inside according to the agreed fields to obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions;
[0120] The second parsing module 604 is used to recursively call a predefined logic determination function, so that the logic determination function performs a logic determination on each layer of query statements according to the logic identifier of each layer of query statements, and obtains a search query matching pattern of each layer of query statements;
[0121] The query module 605 is used to input the search query matching pattern and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and return the query results to the user.
[0122] The beneficial effects obtained by the device provided in the embodiments of this specification are consistent with the beneficial effects obtained by the above method and will not be repeated here.
[0123] Reference Figure 7 As shown, based on the above-mentioned multi-condition query method based on ElasticSearch, a computer device 702 is also provided in an embodiment of this specification, wherein the above-mentioned method runs on the computer device 702. The computer device 702 may include one or more processors 704, such as one or more central processing units (CPUs), each of which may implement one or more hardware threads. The computer device 702 may also include any memory 706, which is used to store any kind of information such as code, settings, data, etc. Non-limitingly, for example, the memory 706 may include any one or more combinations of the following: any type of RAM, any type of ROM, flash memory device, hard disk, optical disk, etc. More generally, any memory may use any technology to store information. Further, any memory may provide volatile or non-volatile retention of information. Further, any memory may represent a fixed or removable component of the computer device 702. In one case, when the processor 704 executes an associated instruction stored in any memory or combination of memories, the computer device 702 may perform any operation of the associated instruction. The computer device 702 also includes one or more drive mechanisms 708 for interacting with any storage, such as a hard disk drive mechanism, an optical disk drive mechanism, etc.
[0124] The computer device 702 may also include an input / output module 710 (I / O) for receiving various inputs (via input devices 712) and for providing various outputs (via output devices 714). A specific output mechanism may include a presentation device 716 and an associated graphical user interface (GUI) 718. In other embodiments, the input / output module 710 (I / O), the input device 712, and the output device 714 may not be included, and the computer device 702 may be used as a computer device in a network. The computer device 702 may also include one or more network interfaces 720 for exchanging data with other devices via one or more communication links 722. One or more communication buses 724 couple the components described above together.
[0125] The communication link 722 may be implemented in any manner, for example, through a local area network, a wide area network (e.g., the Internet), a point-to-point connection, etc., or any combination thereof. The communication link 722 may include any combination of hardwired links, wireless links, routers, gateway functions, name servers, etc. governed by any protocol or combination of protocols.
[0126] Corresponding to Figures 1 to 5 In addition to the method shown, an embodiment of the specification also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above method are executed.
[0127] The embodiment of the present specification also provides a computer-readable instruction, wherein when the processor executes the instruction, the program therein causes the processor to execute the following Figures 1 to 5 The method shown.
[0128] The embodiments of the present specification also provide a computer program product, including at least one instruction or at least one program, wherein the at least one instruction or the at least one program is loaded and executed by a processor to implement the following Figures 1 to 5 The method shown.
[0129] It should be understood that in the various embodiments of this specification, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this specification.
[0130] It should also be understood that in the embodiments of this specification, the term "and / or" is only a description of the association relationship of the associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this specification generally indicates that the associated objects before and after are in an "or" relationship.
[0131] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in this specification can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this specification.
[0132] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0133] In the several embodiments provided in this specification, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, or it can be an electrical, mechanical or other form of connection.
[0134] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of this specification.
[0135] In addition, each functional unit in each embodiment of this specification may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0136] If the integrated unit is implemented in the form of 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, the technical solution of this specification is essentially or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of this specification. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0137] Specific embodiments are used in this specification to illustrate the principles and implementation methods of this specification. The description of the above embodiments is only used to help understand the methods and core ideas of this specification. At the same time, for those skilled in the art, according to the ideas of this specification, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on this specification.
Claims
1. A multi-condition query method based on ElasticSearch, characterized in that: The method comprises: Receive user-defined query conditions; Convert the custom query condition into a query statement in json format, wherein the query statement contains multiple layers of logical nested relationships; Parse the query statement from outside to inside according to the agreed fields to obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions; Recursively calling a predefined logic determination function so that the logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements to obtain a search query matching pattern of each layer of query statements; The search query matching pattern and corresponding sub-conditions of each layer of query statements are input into the ElasticSearch search engine for query, and the query results are returned to the user.
2. The method according to claim 1, characterized in that: The step of converting the custom query condition into a query statement in JSON format includes: Parsing the user-defined query condition, and converting the user-defined query condition into a predefined data structure according to the parsed content, wherein the data structure includes a field name, an operator, a field value, and a logical identifier; Constructing a query condition tree according to the data structure; The query condition tree is traversed, and the query condition tree is converted into a json statement using a json module.
3. The method according to claim 1, characterized in that The logic identifier includes an AND identifier and an OR identifier, and the search query matching mode of each layer of query statements includes: If the logic identifier is AND, the search query matching mode of each layer of query statements includes a full field matching mode, a partial field matching mode, and a time range matching mode; If the logic identifier is OR, the search query matching mode of each layer of query statements is any one of a full field matching mode, a partial field matching mode, or a time range matching mode.
4. The method according to claim 1, characterized in that: Before parsing the query statement from outside to inside according to the agreed fields, the method further includes: Defining a query object structure, wherein the query object structure includes an object type and an object attribute; Extract key information from the query conditions; Extracting semantic information from the key information using a semantic information extraction model to obtain a semantic representation; Mapping the semantic representation to the query object structure to obtain mapping information; A query object is constructed according to the mapping information.
5. The method according to claim 1, characterized in that The agreed fields include an AND field and an OR field. The query statement is parsed from the outside to the inside according to the agreed fields to obtain a logical identifier of each layer of the query statement and a corresponding sub-condition, including: Determining the logical structure of the query statement; Parsing starts from the outermost query statement, determining the agreed fields contained in the outermost query statement as logical identifiers, and obtaining sub-conditions contained in the outermost query statement; Determine whether the sub-conditions included in the outermost query statement include an agreed field, and if so, determine the corresponding agreed field as a logical identifier of the second outer query statement; Repeat the above steps until the sub-conditions contained in the query statement at the current level do not contain the agreed field, and obtain the logical identifier of the query statement at each level and the corresponding sub-conditions.
6. The method according to claim 1, characterized in that The recursive calling of a predefined logic determination function so that the logic determination function performs logic determination on each layer of query statements according to the logic identifier of each layer of query statements includes: Determine whether the logical identifier in each layer of query statements is empty, and if so, exit the recursion; If not, the search query matching mode of the query statement is determined according to the logical identifier.
7. A multi-condition query device based on ElasticSearch, characterized in that: The device comprises: A receiving module, used for receiving user-defined query conditions; A conversion module, used to convert the custom query condition into a query statement in JSON format, wherein the query statement contains multiple layers of logical nested relationships; A first parsing module, used to parse the query statement from the outside to the inside according to the agreed fields, and obtain the logical identifier of each layer of the query statement and the corresponding sub-conditions; A second parsing module is used to recursively call a predefined logic determination function, so that the logic determination function performs a logic determination on each layer of query statements according to the logic identifier of each layer of query statements, and obtains a search query matching pattern of each layer of query statements; The query module is used to input the search query matching pattern and corresponding sub-conditions of each layer of query statements into the ElasticSearch search engine for query, and return the query results to the user.
8. A computer 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, the method according to any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
10. A computer program product, characterized in that The method comprises at least one instruction or at least one program, wherein the at least one instruction or the at least one program is loaded and executed by a processor to implement the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
ElasticSearch query method, device and equipment and readable storage medium
CN113626470A
Searching method and electronic equipment
CN117056383A
Cited By
Method and system for reserving query sequence based on ElasticSearch
CN118939858A
