An ElasticSearch retrieval method implemented based on Java annotations
By defining Java annotations and using recursive parsing and API calls, the rapid search of ElasticSearch is realized, solving the problems of high usage threshold and low development efficiency when using ElasticSearch for data retrieval in the existing technology, improving development efficiency and supporting infinite nested search logic.
Patent Information
- Application Number
- CN202411876363.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2044-12-19
AI Technical Summary
In the prior art, developers need to assemble complex query APIs or query templates when using ElasticSearch for data retrieval, resulting in high usage thresholds and low development efficiency.
By defining Java annotations, each Java annotation corresponds to an ElasticSearch query syntax. Developers can easily annotate search fields and generate ElasticSearch query statements using recursive parsing and API calls to achieve fast retrieval.
Reduces developers' learning costs for ElasticSearch, reduces the workload of assembling query statements, improves development efficiency, and supports infinite nesting of retrieval logic.
Smart Images

Figure CN119322792B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data retrieval, and particularly to an ElasticSearch retrieval method implemented based on Java annotations. Background Art
[0002] In the process of the rapid development of the Internet, with the continuous expansion of the company scale and the continuous upgrade of the business system, it is very common for business data to quickly expand to hundreds of millions or billions. How to quickly retrieve such a large amount of data has become a common problem in the industry. ElasticSearch is a data storage that can support the storage and retrieval of massive data. Although ElasticSearch has excellent performance, for developers, the learning cost is relatively high and the complexity of implementing retrieval is relatively large. How to enable developers to get started and use it as quickly as possible has become the key.
[0003] Currently, the clients provided in the industry for ElasticSearch retrieval include spring-data-elasticSearch, bboos, restHighLevelClient, etc. Developers have relatively high usage thresholds, and they need to assemble query APIs or query templates by themselves, which all involve a large amount of work and low development efficiency. Summary of the Invention
[0004] In order to solve the above problems and enable developers to use ElasticSearch more quickly and conveniently, the present invention proposes a method for retrieving ElasticSearch based on annotations. Developers can get started and use it in a lightweight manner without any ElasticSearch foundation.
[0005] To achieve the above object, the technical solution adopted by the present invention is as follows:
[0006] An ElasticSearch retrieval method implemented based on Java annotations, comprising the following steps:
[0007] Step 1, define Java annotations, and each Java annotation corresponds to an ElasticSearch query syntax;
[0008] Step 2, define the API of each Java annotation;
[0009] Step 3, obtain a Java object A. A Java object A contains one or more retrieval fields. According to the ElasticSearch query syntax of the retrieval fields, select the corresponding Java annotation to mark on each retrieval field;
[0010] Step 4: Recursively parse the retrieval fields marked with Java annotations in Java object A, call the API corresponding to the Java annotation to generate an Elasticsearch query statement, and obtain the Elasticsearch retrieval result through the Elasticsearch query statement;
[0011] Step 5: Parse the retrieval result of Elasticsearch into Java object B and return it.
[0012] Further, in step 2, each Java annotation encapsulates an API: the input parameter field of the API is the retrieval field, the value is the retrieval field value, and each API corresponds to a parsing method.
[0013] Further, the method body of the parsing method is to first obtain the Java annotation of the retrieval field, then obtain the field name of the retrieval field according to the Java annotation, and then assemble the field name and value of the retrieval field into an Elasticsearch query statement.
[0014] Further, step 4 includes the following operations: Step 41: Use Java reflection to obtain all retrieval fields of the Java retrieval object; Step 42: Traverse all retrieval fields, and each retrieval field calls the parsing method of the corresponding API according to the marked Java annotation to assemble the Elasticsearch retrieval statement; Step 43: Obtain the Elasticsearch retrieval result through the Elasticsearch retrieval statement.
[0015] Further, step 41 includes: calling the getclass() function of the object class to obtain all retrieval fields of the Java retrieval object, and calling the Class.getDeclaredFields() function to obtain all attributes in the retrieval fields.
[0016] Further, in step 41, when the obtained retrieval field is a nested object, step 41 is iteratively repeated.
[0017] Further, in step 42, if the retrieval field is a parent class retrieval field, recursively process the Java annotation of the parent class retrieval field, and call the corresponding API according to the Java annotation marked by the top-level parent class retrieval field after recursion.
[0018] Further, step 5 includes the following operations: Step 51: Extract the hits array returned by the Elasticsearch retrieval result; Step 52: Traverse the hits array; Step 53: Serialize each hits array element into json data, and then parse the json data into Java object B.
[0019] Compared with the prior art, the present invention has the following beneficial effects:
[0020] Based on Java annotations, the present invention quickly realizes the retrieval of ElasticSearch, saving the time cost for developers to learn ElasticSearch. There is no need to assemble complex ElasticSearch query statements by themselves, greatly improving the development efficiency. The present invention realizes the infinite nesting of retrieval logic by recursively reusing the parsing kernel of Java annotations. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 is a flowchart of the method of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0022] TERMINOLOGY EXPLANATION:
[0023] ElasticSearch: An open-source highly scalable distributed full-text search engine.
[0024] API: Application Programming Interface.
[0025] Java object: An instance of a class, an entity with state (attributes) and behavior (methods).
[0026] In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the scope of protection of the present invention. In the description of the present invention, it should be noted that the terms "A", "B", etc. are only used to distinguish different objects, rather than to describe a specific order.
[0027] As Figure 1 shown, an embodiment of a method for implementing ElasticSearch retrieval based on Java annotations provided by the present invention includes the following steps:
[0028] Step 1, define Java annotations, and each Java annotation corresponds to an ElasticSearch query syntax;
[0029] Step 2, define the API for each Java annotation;
[0030] Step 3, obtain Java object A. A Java object A contains one or more retrieval fields. According to the ElasticSearch query syntax of the retrieval fields, select the corresponding Java annotation to mark on each retrieval field;
[0031] Step 4: Recursively parse the retrieval fields marked with Java annotations in Java object A, call the API corresponding to the Java annotation to generate an ElasticSearch query statement, and obtain the ElasticSearch retrieval result through the ElasticSearch query statement;
[0032] Step 5: Parse the retrieval result of ElasticSearch into Java object A and return it.
[0033] The present invention realizes ElasticSearch retrieval based on Java annotations, reduces the usage threshold of ElasticSearch for developers, and improves development efficiency.
[0034] The query syntax for retrieving data in ElasticSearch mainly includes the token-based retrieval match series syntax, the exact retrieval term, must, should series syntax, the fuzzy retrieval wildcard, the parent-child document series nested, has_child series syntax, and the aggregation retrieval series syntax, etc. The present invention defines the above query syntax as Java annotations, and each Java annotation corresponds to an ElasticSearch query syntax, as shown in Table 1 below.
[0035] Table 1
[0036]
[0037] ElasticSearch provides rich APIs for external use. The present invention encapsulates each Java annotation defined in Step 1 into an API for ElasticSearch to use. The input parameter field of the API is the retrieval field, and the value is the value of the retrieval field. Each API corresponds to a parsing method. The method body of the parsing method is to first obtain the Java annotation of the retrieval field, then obtain the field name of the retrieval field according to the Java annotation, and then assemble the field name of the retrieval field and the value of the retrieval field into an ElasticSearch query statement.
[0038] Generally, users will retrieve data through the front-end page. The front-end passes the fields used by the user for retrieval to the back-end. A single user retrieval may contain multiple retrieval fields, and there may be significant logical differences between each retrieval field, that is, different query grammars. For example, retrieve mobile phones with the brand **, the color is red, the price is between 3000 and 5000, the release time is after August 2022, and sort by the number of positive reviews in descending order. Java object A will receive the above multiple retrieval fields. Each field is marked with different Java annotations according to different business logics. For example, if the brand is **, the retrieval field "brand" is marked with @EsEquals. If the release time is after August 2022, the retrieval field "release time" is marked with @EsRange(lt=true), where lt=true means to retrieve data greater than the value of the retrieval field.
[0039] After the parsing module of the back-end obtains Java object A marked with Java annotations, it uses the reflection function of Java to parse one by one the retrieval fields with Java annotations in Java object A. Specifically, it includes: calling the getclass() function of the object class to obtain all the retrieval fields of the Java retrieval object, and calling the Class.getDeclaredFields() function to obtain all the attributes in the retrieval fields. Java object A supports the inheritance of the parent class by the parent class again, and indexing needs to be carried out recursively to obtain the retrieval fields. When the parent class is Object, it means that the top-level parent class has been reached, the parent class is obtained, and then assigned to itself to end the iteration. Each retrieval field supports nested objects. For example, object B can be nested in object A, and object C can be nested in object B. In theory, infinite nesting is supported. Different logical retrieval fields can exist in each level of nested objects. If it is a nested object, then the nested object needs to be iteratively parsed, repeating the above steps. Traverse all the retrieval fields, and each retrieval field calls the parsing method of the corresponding API according to the marked Java annotation to assemble the retrieval statement of ElasticSearch; finally, the retrieval result of ElasticSearch is obtained through the retrieval statement of ElasticSearch. If the retrieval field is a parent-class retrieval field, recurse on the Java annotation of the parent-class retrieval field, and call the corresponding API according to the Java annotation marked on the top-level parent-class retrieval field after recursion. When the Java annotation is the annotation of the recursive field, such as the parent-child document retrieval annotation @EsHasChildRelation, recursive parsing is carried out. The reason for the recursion is some complex retrieval scenarios, such as parent-child documents, which need to meet both the retrieval fields of the main document and the retrieval fields of the sub-documents associated with the main document. The Java annotation of the retrieval field matches the API defined in step 2 to assemble the retrieval statement of ElasticSearch.
[0040] The search results returned by ElasticSearch are in a structure unique to ElasticSearch and need to be parsed into structured data for front-end display. The following operations are included: extracting the hits array returned by the ElasticSearch search results; traversing the hits array; serializing each element of the hits array into JSON data and then parsing the JSON data into the Java object B. Parsing and serialization can be implemented using existing JSON tools.
[0041] Finally, it should be noted that the above embodiments are only relatively preferred embodiments of the present invention to illustrate the technical solutions of the present invention, rather than limiting it, and certainly not limiting the patent scope of the present invention; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention; that is to say, any meaningless changes or polishings made in the main design concept and spirit of the present invention, as long as the technical problems solved are still the same as those of the present invention, should be included in the protection scope of the present invention; in addition, directly or indirectly applying the technical solutions of the present invention to other related technical fields shall likewise be included in the patent protection scope of the present invention.
Claims
1. An ElasticSearch retrieval method based on Java annotations, characterized in that: The following steps are involved: Step 1: Define Java annotations. Each Java annotation corresponds to an ElasticSearch query syntax. Step 2: Define the API for each Java annotation. Step 3: Get Java object A. A Java object A contains one or more search fields. According to the ElasticSearch query syntax of the search field, select the corresponding Java annotation mark on each search field. Step 4: Recursively parse the search fields marked with Java annotations in Java object A, call the API corresponding to the Java annotation, generate an ElasticSearch query statement, and obtain the ElasticSearch search results through the ElasticSearch query statement; Step 5: Parse the search results of ElasticSearch into Java object B and return it; Each Java annotation encapsulates an API: the API input parameter field is the search field, value is the search field value, and each API corresponds to a parsing method; The method body of the parsing method first obtains the Java annotation of the search field, then obtains the field name of the search field according to the Java annotation, and then assembles the field name of the search field and the search field value into an ElasticSearch query statement.
2. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 1, it is characterized in that: Step 4 includes the following operations: Step 41, use Java reflection to obtain all search fields of the Java search object; Step 42, traverse all search fields, and each search field calls the parsing method of the corresponding API according to the annotated Java annotation to assemble the ElasticSearch search statement; Step 43, obtain the ElasticSearch search result through the ElasticSearch search statement.
3. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 2, it is characterized in that: Step 41 includes: calling the getclass() function of the object class to obtain all search fields of the Java search object, and calling the Class.getDeclaredFields() function to obtain all attributes in the search field.
4. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 3, it is characterized in that: In step 41 , when the obtained search field is a nested object, step 41 is repeated iteratively.
5. The ElasticSearch retrieval method based on Java annotation according to claim 2, characterized in that: In step 42, if the search field is a parent class search field, the Java annotation of the parent class search field is recursively searched, and the corresponding API is called according to the Java annotation annotated by the top-level parent class search field after the recursion.
6. The ElasticSearch retrieval method based on Java annotation according to claim 1, characterized in that: Step 5 includes the following operations: Step 51, extracting the hits array returned by the search results of ElasticSearch; Step 52, traversing the hits array; Step 53, serializing each hits array element into json data, and then parsing the json data into Java object B.
Citation Information
Patent Citations
Method and device for accessing search engine
CN108959294A
Java object attribute alias acquisition method based on reflection and annotation
CN117492713A
Object serialization method and device, electronic equipment and storage medium
CN117950637A