ElasticSearch retrieval method realized based on Java annotation
By defining Java annotations and recursive parsing, ElasticSearch query statements are generated, the problem of high threshold for ElasticSearch search developers is solved, rapid retrieval is achieved, development efficiency is improved, and infinite nesting logic is supported.
Patent Information
- Application Number
- CN202411876363.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-01-17
- Estimated Expiration
- 2044-12-19
AI Technical Summary
In the prior art, the developer threshold for ElasticSearch search is high, and it is necessary to assemble complex query APIs by itself, resulting in low development efficiency.
By defining Java annotations, each annotation corresponds to an ElasticSearch query syntax, using the search fields in the Java object to annotate the corresponding annotations, recursively parsing and generating ElasticSearch query statements, realizing fast retrieval.
It lowers the threshold for developers to use ElasticSearch, saves learning time costs, improves development efficiency, and supports infinite nesting of retrieval logic.
Smart Images

Figure CN119322792A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data retrieval, and in particular to an ElasticSearch retrieval method based on Java annotation implementation. Background Art
[0002] In the process of rapid development of the Internet, with the continuous expansion of company scale and continuous upgrading of business systems, it is very common for business data to quickly expand to hundreds of millions or even billions. How to quickly retrieve such large-scale data has become a common problem in the industry. ElasticSearch is a data storage that supports massive data storage and retrieval. Although ElasticSearch has excellent performance, for developers, the cost of getting started is 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 for ElasticSearch retrieval provided by the industry include spring-data-elasticSearch, bboos, restHighLevelClient, etc. Developers have a high threshold for use and need to assemble query APIs or query templates by themselves, which is a large workload and has 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, which developers can use in a lightweight way without any ElasticSearch foundation.
[0005] To achieve the above purpose, the technical solution adopted by the present invention is as follows: An ElasticSearch retrieval method based on Java annotations includes the following steps: 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.
[0006] Furthermore, in step 2, each Java annotation encapsulates an API: the input parameter field of the API is the search field, the value is the search field value, and each API corresponds to a parsing method.
[0007] Furthermore, 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.
[0008] Furthermore, step 4 includes the following operations: step 41, using java reflection to obtain all search fields of the Java search object; step 42, traversing all search fields, each search field calls the parsing method of the corresponding API according to the annotated Java annotation, and assembles the ElasticSearch search statement; step 43, obtaining the ElasticSearch search result through the ElasticSearch search statement.
[0009] Furthermore, 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.
[0010] Furthermore, in step 41, when the obtained search field is a nested object, step 41 is repeated iteratively.
[0011] Furthermore, in step 42, if the search field is a parent class search field, the Java annotation of the parent class search field is recursively retrieved, and the corresponding API is called according to the Java annotation annotated on the top-level parent class search field after the recursion.
[0012] Furthermore, 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.
[0013] Compared with the prior art, the present invention has the following beneficial effects: The present invention quickly implements ElasticSearch retrieval based on Java annotations, saves developers time cost of learning ElasticSearch, does not need to assemble complex ElasticSearch query statements by themselves, and greatly improves development efficiency. The present invention realizes infinite nesting of retrieval logic by recursively reusing the parsing kernel of Java annotations. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0015] Terminology explanation: ElasticSearch: An open source, highly scalable distributed full-text search engine.
[0016] API: Application Programming Interface.
[0017] Java object: An instance of a class, an entity with state (attributes) and behavior (methods).
[0018] In order to make the purpose, technical solutions and advantages of the present invention clearer, the present invention is further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in the field without making creative work are 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.
[0019] like Figure 1 As shown, an embodiment of an ElasticSearch retrieval method based on Java annotation provided by the present invention includes the following steps: 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 A and return it.
[0020] The present invention implements ElasticSearch retrieval based on Java annotations, reduces the threshold for developers to use ElasticSearch, and improves development efficiency.
[0021] ElasticSearch query syntax for retrieving data mainly includes the match series syntax for word segmentation retrieval, the term, must, should series syntax for precise retrieval, the wildcard series syntax for fuzzy retrieval, the nested and has_child series syntax for parent-child documents, and the aggregate retrieval series syntax. The present invention defines Java annotations for the above query syntaxes, and each Java annotation corresponds to an ElasticSearch query syntax, as shown in Table 1 below.
[0022] Table 1
[0023] ElasticSearch provides a rich API for external retrieval. The present invention encapsulates each Java annotation defined in step 1 into an API for use by ElasticSearch. The input parameter field of the API is the retrieval field, and the value is the retrieval field value. Each API corresponds to a parsing method. The method body of the parsing method first obtains the Java annotation of the retrieval field, then obtains the field name of the retrieval field according to the Java annotation, and then assembles the field name of the retrieval field and the retrieval field value into an ElasticSearch query statement.
[0024] Usually users will retrieve data through the front-end page, and the front-end will pass the fields used by the user to retrieve to the back-end. A user's search may contain multiple search fields, and each search field will have huge logical differences, that is, different query syntax. For example, to retrieve mobile phones with a brand of **, a red color, a price between 3,000 and 5,000, and a launch time greater than August 2022, sorted in descending order by positive reviews, Java object A will receive the above multiple search fields, and each field will be annotated with different Java annotations according to different business logics. For example, if the brand is **, the search field "Brand" is annotated with @EsEquals, and if the launch time is greater than August 2022, the search field "Launching time" is annotated with @EsRange(lt=true), where lt=true means to retrieve data greater than the search field value.
[0025] After the back-end parsing module obtains the Java object A marked with Java annotations, it uses Java's reflection function to parse the search fields with Java annotations in the Java object A one by one, including: calling the getclass() function of the object class to obtain all the search fields of the Java search object, calling the Class.getDeclaredFields() function to obtain all the attributes in the search field. Java object A supports the inheritance of the parent class by the parent class. The index needs to be recursive to obtain the search field. When the parent class is Object, it means that the parent class at the top level has been reached. The parent class is obtained and then assigned to itself, and the iteration ends. Each search field supports nested objects. For example, the B object can be nested in the A object, and the C object can be nested in the B object. In theory, it supports unlimited nesting, and each layer of nested objects can have search fields with different logics. If it is a nested object, then the nested object needs to be iteratively parsed and the above steps are repeated. Traverse all search fields, and each search field calls the corresponding API parsing method according to the marked Java annotations to assemble the ElasticSearch search statement; finally, the ElasticSearch search result is obtained through the ElasticSearch search statement. 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 recursion. When the Java annotation is an annotation of a recursive field, such as the parent-child document search annotation @EsHasChildRelation, recursive parsing is performed. The reason for recursion is that some complex search scenarios, such as parent-child documents, must satisfy both the search fields of the main document and the search fields of the child documents associated with the main document. The Java annotation of the search field matches the API defined in step 2, and the search statement of ElasticSearch is assembled.
[0026] The search results returned by ElasticSearch are unique to ElasticSearch and need to be parsed to obtain structured data for front-end display. The following operations are included: extracting the hits array returned by ElasticSearch search results; traversing the hits array; serializing each hits array element into json data, and then parsing the json data into Java object B. Parsing and serialization can be achieved using existing json tools.
[0027] Finally, it should be noted that the above embodiments are only preferred embodiments of the present invention to illustrate the technical solutions of the present invention, rather than limiting them, and certainly not limiting the patent scope of the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions recorded in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. 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. In other words, any changes or modifications made to the main design concept and spirit of the present invention that have no substantive significance, and the technical problems they solve are still consistent with the present invention, should be included in the protection scope of the present invention. In addition, the direct or indirect application of the technical solutions of the present invention in other related technical fields is also 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.
2. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 1, it is characterized in that: In step 2, each Java annotation encapsulates an API: the API input parameter field is the search field, the value is the search field value, and each API corresponds to a parsing method.
3. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 2, it is characterized in that: 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.
4. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 2, 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.
5. According to the ElasticSearch retrieval method based on Java annotation implementation according to claim 4, 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.
6. The ElasticSearch retrieval method based on Java annotation according to claim 5, characterized in that: In step 41 , when the obtained search field is a nested object, step 41 is repeated iteratively.
7. The ElasticSearch retrieval method based on Java annotation according to claim 4 is 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.
8. 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 configuration for generating query condition in annotation way
CN103020064A
Method and device for accessing search engine
CN108959294A
Method and device for realizing complex query of mass data based on Elasticsearch and medium
CN110543517A
Json character string generation method and device, storage medium and processor
CN110825389A
Cache plug-in annotator and annotation method
CN112507067A