A data query method and device based on ElasticSearch

By receiving data query requests and determining the query type according to the identification method, and generating ElasticSearch query statements, the problems of complex and poor maintainability of data query methods in the prior art are solved, and the effect of simplifying the query process and improving efficiency is achieved.

CN113760969BActive Publication Date: 2025-02-21BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202110185189.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-02-10
Publication Date
2025-02-21
Estimated Expiration
2041-02-10

AI Technical Summary

Technical Problem

The existing data query methods based on ElasticSearch have problems such as complex syntax, poor maintainability and poor readability, especially when query conditions change, the method definition needs to be modified.

Method used

By receiving data query requests, obtaining query conditions and identification methods, determining the query type (combined query or complex query) based on the identification method, and generating ElasticSearch query statements using the corresponding parsing rules, without writing code.

Benefits of technology

It simplifies the complexity of data query, improves data query efficiency, and does not need to modify the method definition when query conditions change, improving maintainability and readability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113760969B_ABST
    Figure CN113760969B_ABST
Patent Text Reader

Abstract

The present invention discloses a data query method and device based on ElasticSearch, and relates to the field of computer technology. A specific implementation of the method includes: receiving a data query request, obtaining a data query condition corresponding to the data query request and an identification method corresponding to the data query request; determining a data query type corresponding to the data query condition according to the identification method corresponding to the data query request; parsing the data query condition using a parsing rule corresponding to the data query type, generating an ElasticSearch query statement corresponding to the data query condition; executing the ElasticSearch query statement, and obtaining a data query result corresponding to the data query condition. This implementation does not require code writing, simplifies data query complexity, and improves data query efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a data query method and device based on ElasticSearch. Background Art

[0002] ElasticSearch is a search server based on Lucene (i.e., an open source full-text search engine toolkit), which provides a distributed multi-user full-text search engine. Currently, you can use the native Java client API provided by ElasticSearch to build queries, or you can use the submodule spring-data-elasticsearch of Spring Data (i.e., an open source framework for simplifying database access and supporting cloud services) to query data.

[0003] However, the existing ElasticSearch-based data query methods have the following problems: First, the method of constructing queries using the native Java client API has complex syntax, is relatively professional, and is prone to errors; second, the method of using spring-data-elasticsearch for data query requires modifying the method definition when conditions change, which has poor maintainability and poor readability as query conditions increase; third, the annotations provided by spring-data-elasticsearch do not support the setting of dynamic DSL (Domain Specific Language) statements, and when the query statement is complex, the readability is poor. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides a data query method and device based on ElasticSearch, which does not require code writing, simplifies data query complexity, and improves data query efficiency.

[0005] To achieve the above objective, according to one aspect of an embodiment of the present invention, a data query method based on ElasticSearch is provided.

[0006] According to an embodiment of the present invention, a data query method based on ElasticSearch includes: receiving a data query request, obtaining a data query condition corresponding to the data query request and an identification method corresponding to the data query request; determining a data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes a conjunction query and a complex query; parsing the data query condition using a parsing rule corresponding to the data query type, generating an ElasticSearch query statement corresponding to the data query condition; and executing the ElasticSearch query statement to obtain a data query result corresponding to the data query condition.

[0007] Optionally, the data query condition includes multiple query items; and the data query condition is parsed using the parsing rules corresponding to the data query type to generate an ElasticSearch query statement corresponding to the data query condition, including: if the data query type is a conjunction query, then the ElasticSearch query statement corresponding to the data query condition is generated according to the field expressions and field values ​​corresponding to the multiple query items; if the data query type is a complex query, the data query condition is parsed using a template engine to generate an ElasticSearch query statement corresponding to the data query condition; wherein, a conjunction query refers to a query in which the relationships between query items are conjunctions, and a complex query refers to a query in which the relationships between query items exist or.

[0008] Optionally, generating an ElasticSearch query statement corresponding to the data query condition based on the field expressions and field values ​​corresponding to the multiple query items includes: for each query item in the multiple query items, parsing the field expression corresponding to each query item to obtain the fields and operators contained in each query item; generating an ElasticSearch query statement corresponding to each query item according to the fields, operators and field values ​​contained in each query item according to the correspondence between the operators and the ElasticSearch query syntax; and combining the ElasticSearch query statements corresponding to each query item to generate an ElasticSearch query statement corresponding to the data query condition.

[0009] Optionally, after generating the ElasticSearch query statement corresponding to each query item, the method further includes: determining the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item according to the annotations corresponding to the fields contained in each query item.

[0010] Optionally, the use of a template engine for parsing to generate an ElasticSearch query statement corresponding to the data query condition includes: parsing the annotation contained in the identification method corresponding to the data query request to obtain a template identifier corresponding to the data query condition; loading a template corresponding to the data query condition according to the template identifier; calling the template engine to generate an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameters corresponding to the data query condition.

[0011] Optionally, determining the data query type corresponding to the data query condition according to the identification method corresponding to the data query request includes: when the identification method corresponding to the data query request contains annotations, determining that the data query type is a complex query; when a method name suffix of the identification method corresponding to the data query request is a suffix corresponding to a conjunction query, determining that the data query type is a conjunction query; when the identification method corresponding to the data query request does not contain annotations and a method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to a conjunction query, determining that the data query request is a non-query operation.

[0012] Optionally, the method further includes: a recognition method corresponding to a custom conjunction query and a complex query.

[0013] To achieve the above objective, according to another aspect of an embodiment of the present invention, a data query device based on ElasticSearch is provided.

[0014] According to an embodiment of the present invention, a data query device based on ElasticSearch includes: an acquisition module, which is used to receive a data query request, and obtain a data query condition corresponding to the data query request and an identification method corresponding to the data query request; a determination module, which is used to determine the data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes a conjunction query and a complex query; a parsing module, which is used to parse the data query condition using the parsing rules corresponding to the data query type, and generate an ElasticSearch query statement corresponding to the data query condition; and an execution module, which is used to execute the ElasticSearch query statement to obtain a data query result corresponding to the data query condition.

[0015] Optionally, the data query condition includes multiple query items; and the parsing module is also used for: if the data query type is a conjunction query, then generating an ElasticSearch query statement corresponding to the data query condition based on the field expressions and field values ​​corresponding to the multiple query items; if the data query type is a complex query, then parsing using a template engine to generate an ElasticSearch query statement corresponding to the data query condition; wherein a conjunction query refers to a query in which the relationships between query items are all conjunctions, and a complex query refers to a query in which the relationships between query items exist or not.

[0016] Optionally, the parsing module is also used to: for each query item in the multiple query items, parse the field expression corresponding to each query item to obtain the fields and operators contained in each query item; generate an ElasticSearch query statement corresponding to each query item according to the correspondence between the operator and the ElasticSearch query syntax, and according to the fields, operators and field values ​​contained in each query item; combine the ElasticSearch query statements corresponding to each query item to generate an ElasticSearch query statement corresponding to the data query condition.

[0017] Optionally, the parsing module is further used to determine the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item according to the annotations corresponding to the fields contained in each query item.

[0018] Optionally, the parsing module is also used to: parse the annotations contained in the identification method corresponding to the data query request to obtain the template identifier corresponding to the data query condition; load the template corresponding to the data query condition according to the template identifier; call the template engine to generate an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameters corresponding to the data query condition.

[0019] Optionally, the determination module is also used to: determine that the data query type is a complex query when the identification method corresponding to the data query request contains an annotation; determine that the data query type is a conjunction query when the method name suffix of the identification method corresponding to the data query request is a suffix corresponding to a conjunction query; and determine that the data query request is a non-query operation when the identification method corresponding to the data query request does not contain an annotation and the method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to a conjunction query.

[0020] Optionally, the device further comprises a definition module, which is used to: customize the identification method corresponding to the conjunction query and the complex query.

[0021] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.

[0022] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the ElasticSearch-based data query method according to an embodiment of the present invention.

[0023] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer readable medium is provided.

[0024] A computer-readable medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, the data query method based on ElasticSearch according to an embodiment of the present invention is implemented.

[0025] An embodiment of the above invention has the following advantages or beneficial effects: it can determine the data query type according to the obtained identification method, that is, obtain the relationship between the query items, and then use the parsing rules corresponding to the data query type to parse the data query conditions, and automatically generate ElasticSearch query statements without writing code. This solves the problem that the existing method of using the native Java client API to build queries requires writing a lot of code, simplifies the complexity of data queries, and does not need to modify the method definition when the data query conditions change. It has strong maintainability and readability, and improves data query efficiency.

[0026] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0028] Figure 1 is a schematic diagram of the main steps of the data query method based on ElasticSearch according to an embodiment of the present invention;

[0029] Figure 2 is a schematic diagram of main steps of determining a data query type according to an embodiment of the present invention;

[0030] Figure 3 It is a schematic diagram of the main process of generating an ElasticSearch query statement if the data query type is a conjunction query according to an embodiment of the present invention;

[0031] Figure 4 It is a schematic diagram of the main process of generating an ElasticSearch query statement if the data query type is a complex query according to an embodiment of the present invention;

[0032] Figure 5 is a schematic diagram of the main process of the data query method based on ElasticSearch according to an embodiment of the present invention;

[0033] Figure 6 is a schematic diagram of main modules of a data query device based on ElasticSearch according to an embodiment of the present invention;

[0034] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0035] Figure 8 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0036] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0037] Figure 1 Schematic diagram of the main steps of the data query method based on ElasticSearch according to an embodiment of the present invention. Figure 1 As shown, the main steps of the data query method based on ElasticSearch may include steps S101 to S104.

[0038] Step S101: receiving a data query request, and obtaining a data query condition corresponding to the data query request and an identification method corresponding to the data query request.

[0039] In step S101, after receiving a data query request, the data query condition corresponding to the data query request and the identification method corresponding to the data query request can be obtained. Among them, the data query condition includes multiple query items, and the multiple query items are connected by query item connectives. Query item connectives include conjunction and disjunction; conjunction is the abstraction of the connective "and" in natural language, let p and q represent any query items, and "p and q" means to query data that satisfies p and q at the same time; disjunction is the logical abstraction of the connective "or" in natural language, let p and q represent any query items, and "p or q" means to query data that satisfies at least one of p and q. The query item consists of fields, operators and field values. For example, if you query {order time>'2020-10-01' and (order status = 'completed' or order status = 'cancelled')}, then "order time>'2020-10-01'", "order status = 'completed'", "order status = 'cancelled'" are query items, and "and" and "or" are query item connectives. For the query item "Order time > '2020-10-01'", the field is order time, the operator is >, and the field value is '2020-10-01'.

[0040] In addition to obtaining the data query conditions, the identification method corresponding to the data query request can also be obtained through the data query request. The identification method can be used to identify the data query type corresponding to the data query conditions.

[0041] Step S102: Determine the data query type corresponding to the data query condition according to the identification method corresponding to the data query request.

[0042] Among them, data query types can include conjunction queries and complex queries. The scenario where the relationships between query items are all "conjunction" is called conjunction query, and the scenario where the relationships between query items include "disjunction" is called complex query. That is, conjunction query refers to a query where the relationships between query items are all conjunctions, and complex query refers to a query where the relationships between query items exist or. In actual scenarios, most queries are conjunction queries, and only a small number of queries are complex queries.

[0043] The identification method is used to identify whether the data query type is a conjunction query or a complex query. As an embodiment of the present invention, the data query method based on ElasticSearch may also include: customizing the identification method corresponding to the conjunction query and the complex query. That is, customize the identification method corresponding to the data query type, so that after receiving the data query request, you can get the identification method corresponding to the data query request, and then determine the data query type corresponding to the data query condition through the obtained identification method, that is, get the relationship between the query items in the data query condition. Specifically, define the method findByQueryBean, the method findByQueryMap, and the method findByTemplate containing the annotation @Query; and define the data query type corresponding to the method findByQueryBean and the method findByQueryMap as conjunction query, and define the data query type corresponding to the method findByTemplate containing the annotation @Query as complex query.

[0044] Therefore, as an embodiment of the present invention, the step S102 of determining the data query type corresponding to the data query condition according to the identification method corresponding to the data query request may include: when the identification method corresponding to the data query request includes annotations, determining that the data query type is a complex query; when the method name suffix of the identification method corresponding to the data query request is a suffix corresponding to a conjunction query, determining that the data query type is a conjunction query; when the identification method corresponding to the data query request does not include annotations, and the method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to a conjunction query, determining that the data query request is a non-query operation. Among them, the suffix corresponding to the conjunction query can be determined according to a custom identification method, such as defining the data query type corresponding to the method findByQueryBean and the method findByQueryMap as conjunction queries, then the suffixes corresponding to the conjunction query are ByQueryBean and ByQueryMap. That is, if the method name suffix of the identification method corresponding to the data query request is ByQueryBean or ByQueryMap, the data query type is determined to be a conjunction query.

[0045] Figure 2 FIG. 1 is a schematic diagram of the main steps of determining the data query type according to an embodiment of the present invention. Figure 2 As shown, the main steps to determine the data query type may include:

[0046] Step S201, obtaining an identification method corresponding to a data query request;

[0047] Step S202, determining whether the identification method contains the annotation @Query, if so, executing step S203, if not, executing step S204;

[0048] Step S203, determining it to be a complex query;

[0049] Step S204, determining whether the method name suffix of the identification method is ByQueryBean or ByQueryMap, if so, executing step S205, if not, executing step S206;

[0050] Step S205, determining it to be a conjunction query;

[0051] Step S206: determine whether the data query request is a non-query operation such as save, update, delete, etc.

[0052] The above steps S201 to S206 are examples of determining the data query type. In the method for determining the data query type, step S204 may be performed first, that is, first determining whether it is a conjunction query.

[0053] Step S103: parse the data query conditions using the parsing rules corresponding to the data query type, and generate an ElasticSearch query statement corresponding to the data query conditions.

[0054] Step S104, execute the ElasticSearch query statement to obtain the data query result corresponding to the data query condition.

[0055] The data query type corresponding to the data query condition is obtained through step S102, and then the data query condition can be parsed using the parsing rules corresponding to the data query type to obtain the ElasticSearch query statement corresponding to the data query condition. Finally, in step S104, the generated ElasticSearch query statement can be executed to obtain the data query result corresponding to the data query condition. In addition, the data query result can also be returned according to the expected result return value type, such as List, PageList, Stream, etc.

[0056] The data query method based on ElasticSearch in the embodiment of the present invention can determine the data query type according to the obtained identification method, that is, obtain the relationship between the query items, and then use the parsing rules corresponding to the data query type to parse the data query conditions, and automatically generate ElasticSearch query statements without writing code. This solves the problem that the existing method of using the native Java client API to build queries requires writing a lot of code, simplifies the complexity of data queries, and does not need to modify the method definition when the data query conditions change. It has strong maintainability and readability, and improves data query efficiency.

[0057] Through step S101 to step S104, it can be obtained that the data query type can include conjunction query and complex query, and different parsing rules need to be used for parsing for different data query types. In other words, the parsing rules corresponding to conjunction query and complex query are different. Next, the parsing rules corresponding to the two queries are described in detail.

[0058] (1) If the data query type is a conjunction query, an ElasticSearch query statement corresponding to the data query condition is generated based on the field expressions and field values ​​corresponding to the multiple query items.

[0059] Since the query item consists of a field (fieldName), an operator (operator), and a field value (value), the passed-in parameters need to include these three parts. However, the actual interface uses entity (attribute-value) and map (key-value) methods to pass parameters. For example, the passed-in parameters are: [{fieldName:orderId,operator:>,value:2222},{fieldName:orderCreateTime,operator:>=,value:2020-12-13},{fieldName:orderCreateTime,operator:<=,value:2020-12-15}]. Obviously, if the ternary relationship of the query condition is reflected in the parameter, the complexity of the parameter will increase.

[0060] In order to reduce complexity, the data query method of the embodiment of the present invention can combine the field (fieldName) and the operator (operator) in the query condition into a field expression (fieldExpression), as follows:

[0061] public class OrderQuery {

[0062] private String orderIdEq; / / orderId is the order number, Eq stands for 'equals'

[0063] private String orderCreateTimeGt; / / orderCreateTime is the order time, Gt stands for 'greater'

[0064] private String orderCreateTimeBetween; / / orderCreateTime is the order time, Between means 'between'

[0065] }

[0066] Thus, the ternary relationship of the query item can be reduced to a binary relationship consisting of a field expression and a field value. For example, the parameter passed in can be changed to {orderIdEq:123456789000,orderCreate TimeBetween:[2020-12-13,2020-12-15}. It can be seen that by binding fields to operators, the ternary relationship of the query item can be reduced to a binary relationship, simplifying the complexity of the query item. Therefore, if the data query type corresponding to the data query condition is a conjunction query, the query items contained in the data query condition can be passed in using a binary relationship, that is, the query item consists of a field expression and a field value, and then the ElasticSearch query statement corresponding to the data query condition can be generated according to the field expressions and field values ​​corresponding to multiple query items.

[0067] As an embodiment of the present invention, generating an ElasticSearch query statement corresponding to a data query condition according to field expressions and field values ​​corresponding to a plurality of query items may include:

[0068] (1) For each query item in the multiple query items, the field expression corresponding to each query item is parsed to obtain the fields and operators contained in each query item. Since the field expression is obtained by combining the field and the operator, the field expression corresponding to each query item can be parsed to obtain the fields and operators contained in each query item, and then the fields, operators and field values ​​contained in each query item can be obtained.

[0069] (2) According to the correspondence between the operator and the ElasticSearch query syntax, and based on the fields, operators, and field values ​​contained in each query item, an ElasticSearch query statement corresponding to each query item is generated. After obtaining the fields, operators, and field values ​​contained in each query item, an ElasticSearch query statement corresponding to each query item can be generated according to the correspondence between the operator and the ElasticSearch query syntax. Among them, the correspondence between the operator and the ElasticSearch query syntax is an existing mature technology and will not be described in detail here.

[0070] As an embodiment of the present invention, after generating the ElasticSearch query statement corresponding to each query item, the data query method based on ElasticSearch may also include: determining the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item according to the annotations corresponding to the fields contained in each query item.

[0071] In addition to the query conditions, Elasticsearch queries also have some additional configuration items that need to be configured, such as query routing, query index range, nested relationships, etc. When using the native Java client API to build a query, it is necessary to write code to perform additional configuration on configuration items such as query routing, query index range, and nested relationships. In the embodiment of the present invention, by using annotations on the fields, the query routing, query index range, and nested relationships can be automatically set when parsing data query conditions, without the need to manually set them by writing code, thereby further simplifying the complexity of data queries and improving data query efficiency. For example, the routing annotation @Routing is set on the field venderId, the index range annotation @GruoField is set on the field createTime, and the field skuList is declared as a nested field through the annotation @Field type FieldType.Nested.

[0072] (3) The ElasticSearch query statements corresponding to each query item are combined to generate an ElasticSearch query statement corresponding to the data query condition.

[0073] Since the data query type is a conjunction query, the relationships between the query items are all conjunctions. Therefore, after obtaining the ElasticSearch query statement corresponding to each query item, the ElasticSearch query statement corresponding to each query item can be directly combined to obtain the ElasticSearch query statement corresponding to the data query condition.

[0074] Figure 31 is a schematic diagram of the main process of generating an ElasticSearch query statement if the data query type is a conjunction query according to an embodiment of the present invention. Figure 3 As shown in the figure, if the data query type is a conjunction query, the main process of generating an ElasticSearch query statement may include:

[0075] Step S301, selecting a query item from multiple query items;

[0076] Step S302, parsing the field expression corresponding to the query item to obtain the fields and operators included in the query item, so as to obtain the fields, operators and field values ​​included in the query item;

[0077] Step S303, according to the correspondence between the operator and the ElasticSearch query syntax, and according to the fields, operators and field values ​​included in the query item, an ElasticSearch query statement corresponding to the query item is generated;

[0078] Step S304, determining the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to the query item according to the annotations corresponding to the fields included in the query item;

[0079] Step S305, determining whether an ElasticSearch query statement corresponding to each query item is generated, if so, executing step S306, if not, executing step S301;

[0080] Step S306, combining the ElasticSearch query statements corresponding to each query item to generate an ElasticSearch query statement corresponding to the data query condition.

[0081] In the data query method based on ElasticSearch in the embodiment of the present invention, if the data query type is a conjunction query, the query items can be passed in using binary relations, that is, the query items are composed of field expressions and field values, which can simplify the complexity of the query items and facilitate user use; in addition, by using annotations on the fields, the query route, query index range and nested relationship can be automatically set without manual setting by writing code, which further simplifies the complexity of data queries and improves data query efficiency.

[0082] (ii) If the data query type is a complex query, the template engine is used to parse and generate an ElasticSearch query statement corresponding to the data query condition.

[0083] When there is a logical OR in the query items in the query conditions, it is very complicated to construct the query whether using the native Java client API or spring-data-elasticsearch, and it is easy to have the situation that the construction process is inconsistent with the expected DSL query statement. In the embodiment of the present invention, the Elasticsearch DSL statement can be dynamically constructed by using a template engine, thereby reducing the complexity of query statement generation. The template engine can be Freemarke, that is, a template engine written in Java language.

[0084] Figure 4 1 is a schematic diagram of the main process of generating an ElasticSearch query statement if the data query type is a complex query according to an embodiment of the present invention. Figure 4 As shown in the figure, if the data query type is a complex query, the main process of generating an ElasticSearch query statement may include:

[0085] Step S401, parsing the annotations contained in the identification method corresponding to the data query request, and obtaining the template identifier corresponding to the data query condition;

[0086] Step S402, loading the Freemarke template corresponding to the data query condition according to the template identifier;

[0087] Step S403, calling the template engine Freemarke, and generating an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameter corresponding to the data query condition.

[0088] If the data query type is a complex query, it means that the identification method corresponding to the data query request contains the annotation @Query, so the template identifier can be obtained by parsing the annotation, such as parsing the annotation @Query("billChargeTemplate") to obtain the template identifier billChargeTemplate. Then, the Freemarke template can be loaded according to the template identifier obtained by parsing. Finally, the template engine Freemarke is called, the query parameters corresponding to the Freemarke template and the data query conditions are passed in, the judgment conditions, loops and other statements in the Freemarke template are compiled, and the ElasticSearch DSL statements corresponding to the data query conditions are dynamically generated. Among them, the query parameters corresponding to the data query conditions refer to the multiple query items contained in the data query conditions. It should be noted that if the data query type is a complex query, the Elasticsearch DSL statements are directly generated dynamically using the template engine, so there is no need to limit the query items to be passed in using binary relationships.

[0089] The ElasticSearch-based data query method of an embodiment of the present invention can dynamically generate Elasticsearch DSL statements through a template engine if the data query type is a complex query, thereby solving the problem that the annotations provided by spring-data-elasticsearch do not support dynamic DSL statement settings, and has strong readability and improved maintainability.

[0090] Figure 5 FIG. 1 is a schematic diagram of the main process of the data query method based on ElasticSearch according to an embodiment of the present invention. Figure 5 As shown, the main process of the data query method based on ElasticSearch can include:

[0091] Step S501, receiving a data query request, obtaining a data query condition corresponding to the data query request and an identification method corresponding to the data query request, wherein the data query condition includes a plurality of query items;

[0092] Step S502, determining the data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes conjunction query and complex query;

[0093] Step S503, parsing the field expression corresponding to each query item to obtain the fields and operators included in each query item, so as to obtain the fields, operators and field values ​​included in each query item;

[0094] Step S504, generating an ElasticSearch query statement corresponding to each query item according to the correspondence between the operator and the ElasticSearch query syntax, and according to the fields, operators, and field values ​​contained in each query item;

[0095] Step S505, determining the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item according to the annotations corresponding to the fields contained in each query item;

[0096] Step S506, combining the ElasticSearch query statements corresponding to each query item to generate an ElasticSearch query statement corresponding to the data query condition;

[0097] Step S507, parsing the annotations contained in the identification method corresponding to the data query request, and obtaining the template identifier corresponding to the data query condition;

[0098] Step S508, loading the template corresponding to the data query condition according to the template identifier;

[0099] Step S509, calling the template engine, generating an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameter corresponding to the data query condition, wherein the ElasticSearch DSL statement is an ElasticSearch query statement;

[0100] Step S510, execute the ElasticSearch query statement to obtain the data query result corresponding to the data query condition.

[0101] The specific implementation method of determining the data query type in step S502 has been described in steps S201 to S206 and will not be described in detail here. In addition, if it is determined that the data query request is a non-query operation, "the request is a non-query operation" can be directly returned.

[0102] Figure 6 Schematic diagram of the main modules of the data query device based on ElasticSearch according to an embodiment of the present invention. Figure 6 As shown, the main modules of the data query device 600 based on ElasticSearch may include: an acquisition module 601, a determination module 602, a parsing module 603 and an execution module 604.

[0103] Among them, the acquisition module 601 can be used to: receive a data query request, obtain the data query conditions corresponding to the data query request and the identification method corresponding to the data query request; the determination module 602 can be used to: determine the data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes conjunction query and complex query; the parsing module 603 can be used to: use the parsing rules corresponding to the data query type to parse the data query condition, and generate an ElasticSearch query statement corresponding to the data query condition; the execution module 604 can be used to: execute the ElasticSearch query statement to obtain the data query result corresponding to the data query condition.

[0104] As an embodiment of the present invention, the data query condition may include multiple query items; and the parsing module 603 can also be used for: if the data query type is a conjunction query, then generating an ElasticSearch query statement corresponding to the data query condition based on the field expressions and field values ​​corresponding to the multiple query items; if the data query type is a complex query, then using the template engine for parsing to generate an ElasticSearch query statement corresponding to the data query condition; wherein, a conjunction query refers to a query in which the relationships between the query items are conjunctions, and a complex query refers to a query in which the relationships between the query items exist or.

[0105] As an embodiment of the present invention, the parsing module 603 can also be used to: for each query item in multiple query items, parse the field expression corresponding to each query item to obtain the fields and operators contained in each query item; according to the correspondence between the operator and the ElasticSearch query syntax, according to the fields, operators and field values ​​contained in each query item, generate an ElasticSearch query statement corresponding to each query item; combine the ElasticSearch query statements corresponding to each query item to generate an ElasticSearch query statement corresponding to the data query condition.

[0106] As an embodiment of the present invention, the parsing module 603 may also be used to determine the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item according to the annotations corresponding to the fields included in each query item.

[0107] As an embodiment of the present invention, the parsing module 603 can also be used to: parse the annotations contained in the identification method corresponding to the data query request to obtain the template identifier corresponding to the data query condition; load the template corresponding to the data query condition according to the template identifier; call the template engine to generate an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameters corresponding to the data query condition.

[0108] As an embodiment of the present invention, the determination module 602 can also be used to: determine that the data query type is a complex query when the identification method corresponding to the data query request contains an annotation; determine that the data query type is a conjunction query when the method name suffix of the identification method corresponding to the data query request is a suffix corresponding to a conjunction query; determine that the data query request is a non-query operation when the identification method corresponding to the data query request does not contain an annotation and the method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to a conjunction query.

[0109] like Figure 6 As shown, the ElasticSearch-based data query 600 of the embodiment of the present invention may further include: a definition module 605. The definition module 605 may be used to: define a recognition method corresponding to a custom conjunction query and a complex query.

[0110] According to the data query device based on ElasticSearch in the embodiment of the present invention, it is possible to determine the data query type according to the obtained identification method, that is, to obtain the relationship between the query items, and then to parse the data query conditions using the parsing rules corresponding to the data query type, and automatically generate ElasticSearch query statements without writing code. This solves the problem that the existing method of using the native Java client API to construct queries requires writing a lot of code, simplifies the complexity of data queries, and does not need to modify the method definition when the data query conditions change. It has strong maintainability and readability, and improves data query efficiency.

[0111] Figure 7 An exemplary system architecture 700 is shown to which an ElasticSearch-based data query method or an ElasticSearch-based data query device according to an embodiment of the present invention can be applied.

[0112] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0113] The user can use the terminal devices 701, 702, 703 to interact with the server 705 through the network 704 to receive or send messages, etc. Various communication client applications can be installed on the terminal devices 701, 702, 703. The terminal devices 701, 702, 703 can be various electronic devices with display screens and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, etc.

[0114] Server 705 can be a server that provides various services, such as a background management server that provides support for users in the process of using terminal devices 701, 702, and 703 to query data (only as an example); for another example, server 705 can complete the data query based on ElasticSearch in an embodiment of the present invention.

[0115] It should be noted that the ElasticSearch-based data query method provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the ElasticSearch-based data query device is generally arranged in the server 705 .

[0116] It should be understood that Figure 7The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0117] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device suitable for implementing an embodiment of the present invention. Figure 8 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0118] like Figure 8 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0119] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.

[0120] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.

[0121] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0122] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0123] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be set in a processor. For example, they may be described as: a processor includes an acquisition module, a determination module, a parsing module, and an execution module. The names of these modules do not, in some cases, constitute limitations on the modules themselves. For example, the acquisition module may also be described as a "module that receives a data query request, obtains data query conditions corresponding to the data query request, and an identification method corresponding to the data query request."

[0124] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: receiving a data query request, obtaining a data query condition corresponding to the data query request and an identification method corresponding to the data query request; determining a data query type corresponding to the data query condition according to the identification method corresponding to the data query request; parsing the data query condition using the parsing rules corresponding to the data query type, generating an ElasticSearch query statement corresponding to the data query condition; executing the ElasticSearch query statement, and obtaining a data query result corresponding to the data query condition.

[0125] According to the technical solution of the embodiment of the present invention, the data query type can be determined according to the obtained identification method, that is, the relationship between the query items can be obtained, and then the data query conditions can be parsed using the parsing rules corresponding to the data query type, and ElasticSearch query statements can be automatically generated without writing code. This solves the problem that the existing method of using the native Java client API to build queries requires writing a lot of code, simplifies the complexity of data queries, and does not need to modify the method definition when the data query conditions change. It has strong maintainability and readability, and improves data query efficiency.

[0126] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A data query method based on ElasticSearch, characterized in that: include: Receive a data query request, and obtain a data query condition corresponding to the data query request and an identification method corresponding to the data query request; Determine the data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes conjunction query and complex query; the process includes: when the identification method corresponding to the data query request includes annotations, determine that the data query type is a complex query; when the method name suffix of the identification method corresponding to the data query request is a suffix corresponding to the conjunction query, determine that the data query type is a conjunction query; when the identification method corresponding to the data query request does not include annotations and the method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to the conjunction query, determine that the data query request is a non-query operation; Parsing the data query condition using the parsing rule corresponding to the data query type to generate an ElasticSearch query statement corresponding to the data query condition; Execute the ElasticSearch query statement to obtain the data query result corresponding to the data query condition.

2. The method according to claim 1, characterized in that The data query condition includes a plurality of query items; and, The method of parsing the data query condition by using the parsing rule corresponding to the data query type and generating an ElasticSearch query statement corresponding to the data query condition includes: If the data query type is a conjunction query, generating an ElasticSearch query statement corresponding to the data query condition according to the field expressions and field values ​​corresponding to the multiple query items; If the data query type is a complex query, the template engine is used to parse and generate an ElasticSearch query statement corresponding to the data query condition; wherein, A conjunction query is a query in which the relationships between query items are all conjunctions, and a complex query is a query in which the relationships between query items exist or not.

3. The method according to claim 2, characterized in that Generating an ElasticSearch query statement corresponding to the data query condition according to the field expressions and field values ​​corresponding to the multiple query items includes: For each query item in the multiple query items, a field expression corresponding to each query item is parsed to obtain fields and operators included in each query item; According to the correspondence between the operator and the ElasticSearch query syntax, and according to the fields, operators and field values ​​contained in each query item, generate an ElasticSearch query statement corresponding to each query item; The ElasticSearch query statements corresponding to each query item are combined to generate an ElasticSearch query statement corresponding to the data query condition.

4. The method according to claim 3, characterized in that After generating the ElasticSearch query statement corresponding to each query item, the method further includes: According to the annotations corresponding to the fields contained in each query item, the query route, query index range and nested relationship of the ElasticSearch query statement corresponding to each query item are determined.

5. The method according to claim 2, characterized in that: The template engine is used to perform parsing to generate an ElasticSearch query statement corresponding to the data query condition, including: Parsing the annotation contained in the identification method corresponding to the data query request to obtain the template identifier corresponding to the data query condition; According to the template identifier, load the template corresponding to the data query condition; The template engine is called to generate an ElasticSearch DSL statement corresponding to the data query condition according to the template corresponding to the data query condition and the query parameter corresponding to the data query condition.

6. The method according to any one of claims 1 to 5, characterized in that: The method further includes: a recognition method corresponding to a custom conjunction query and a complex query.

7. A data query device based on ElasticSearch, characterized in that: include: An acquisition module, used to receive a data query request, and acquire a data query condition corresponding to the data query request and an identification method corresponding to the data query request; A determination module is used to determine the data query type corresponding to the data query condition according to the identification method corresponding to the data query request, wherein the data query type includes conjunction query and complex query; the process includes: when the identification method corresponding to the data query request includes annotations, determining that the data query type is a complex query; when the method name suffix of the identification method corresponding to the data query request is a suffix corresponding to the conjunction query, determining that the data query type is a conjunction query; when the identification method corresponding to the data query request does not include annotations and the method name suffix of the identification method corresponding to the data query request is not a suffix corresponding to the conjunction query, determining that the data query request is a non-query operation; A parsing module, used to parse the data query condition by using the parsing rule corresponding to the data query type, and generate an ElasticSearch query statement corresponding to the data query condition; The execution module is used to execute the ElasticSearch query statement to obtain the data query result corresponding to the data query condition.

8. An electronic device, characterized in that: include: one or more processors; A storage device, used for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-6.

9. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Data query method and device

    CN110968679A