elasticsearch-query-builder is used for building elasticsearch query DSL. if there are so many query conditions, you won't build so complex elasticsearch DSL like before, and a config file is just enough by using elasticsearch-query-builder. I believe that it will help you make your code simple and easy-understand.
At the first of all, create a config file following elasticsearch-query-builder rules just like the sample below. It is a standard json file.
{
"index": "user_portrait",
"type": "docs",
"from": "${from}",
"size": "10",
"query_type": "terms_level_query",
"terms_level_query": {
"terms_level_type": "term_query",
"term_query": {
"value": "${value}",
"key": "key",
"boost": 2
}
},
"aggregations": [
{
"aggregation_type": "terms",
"name": "",
"field": "field",
"sub_aggregations": {
"aggregation_type": "terms",
"name": "sub",
"field": "field",
"size": "${size.value}",
"sort": "asc",
"sort_by": "_count"
}
}
],
"highlight":{
"fields": [
{
"field": "content",
"number_of_fragment": 2,
"no_match_size": 150
}
],
"pre_tags":["<em>"],
"post_tags":["</em>"]
},
"sort": [
"_score",
{
"field": "age",
"order": "asc"
}
]
}
Here are the config file example.
There are three query_type defined in elasticsearch-query-builder,and they can't be used together.
terms_level_query.
The
terms_level_queryoperate on the exact terms that are stored in the inverted index.These queries are usually used for structured data like numbers, dates, and enums, rather than full text fields. Alternatively, they allow you to craft low-level queries, foregoing the analysis process.
It containsterm_query,terms_query,range_query,exists_queryand so on.
text_level_query.
The
text_level_queryqueries are usually used for running full text queries on full text fields like the body of an email. They understand how the field being queried is analyzed and will apply each field’s analyzer (or search_analyzer) to the query string before executing.
It containsmatch_query,multi_match_query,query_string,simple_query_stringand so on.
bool_level.
A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence.
The occurrence types are: must,filter,should,must_not.In each types ,terms_level_queryandtext_level_queryis involved.
In addition to parsing config file , elasticsearch-query-builder parser parameters from JSONObject(alibaba fastjson object).We use the form of ${} to indicate that the field needs to be fetched from an external data source, just as ${a} indicates that we get a value from a field in Jsonobject. If you need to get data from a deeper level of JSON, just use. to represents a hierarchy, such as ${a.b.c}.
If it is a range query, the JSON data must be a string in [a, b] format, a and b can be empty, but , can't be.
clone this project and execute 'mvn package' and just use it as a jar file.
elasticsearch-query-builder is available under the MIT license. See the LICENSE file for more info.