es查询语句拼接 java_fast-es-query-builder:迅速构建ElasticSearch查询语句,甚至可以不写一行实现代码...

fast-elasticsearch-query-builder

A fast way to build ElasticSearch query dsl string, even without writing implement code.

Read this in other languages: English, 简体中文

Brief

You can integrate it in your project with these steps:

Complete the search criteria DTO MySearchCirteria(add correct @annotation on the field)

public class MySearchCirteria {

@Must

@Match

private String name;

}

Complete the query builder class MyQueryBuilder extending the BaseQueryBuilder, with specifing MySearchCirteria as the generics class

public class MyQueryBuilder extends BaseQueryBuilder {

}

Invoke MyQueryBuilder#build to build the query string

public class TestQueryBuilder {

public static void main(String[] args) {

// instance MyQueryBuilder

MyQueryBuilder myQueryBuilder = new MyQueryBuilder();

// instance MySearchCriteria

MySearchCriteria mySearchCriteria = new MySearchCriteria();

mySearchCriteria.setName("jack");

// invoke build method

String dsl = myQueryBuilder.build(mySearchCriteria);

// enjoy it!

System.out.println(dsl);

}

}

result

{

"from": 0,

"size": 20,

"query": {

"bool": {

"must": [

{

"match": {

"name": {

"query": "jack",

"operator": "OR",

"prefix_length": 0,

"max_expansions": 50,

"fuzzy_transpositions": true,

"lenient": false,

"zero_terms_query": "NONE",

"auto_generate_synonyms_phrase_query": true,

"boost": 1

}

}

}

],

"adjust_pure_negative": true,

"boost": 1

}

}

}

fast-elasticsearch-query-builder generates dsl based on org.elasticsearch-elasticsearch-6.3.0

Instruction

Annotation Instruction

fast-elasticsearch-query-builder privides two kinds of annotation to build query string: Function Annotation and Query Annotation.

Function Annotation

Set query properties.

Annotation

Field Type

Function

Parameters

@Higilighters

Collection

set highlight

type: the highlight type

@PageNo

Integer

set from

-

@PageSize

Integer

set size

-

@Sort

List

set sort, you should implement Sortable interface

-

@Source

Collection

set _source.includes

-

from + size <= max_result_window

Set aggregation

Annotation

Field Type

Function

Parameters

@CardinalityAggregation

Boolean

set cardinality aggregation

name: aggregation name

field: aggregation field

precisionThreshold: precision threshold

@ExtendedStatsAggregation

Boolean

set extended stats aggregation

name: aggregation name

field: aggregation field

@StatsAggregation

Boolean

set stats aggregation

name: aggregation name

field: aggregation field

@TermsAggregation

Integer

set terms aggregation

name: aggregation name

field: aggregation field

maxSize: max value of the field

order: buckets' order

executionHint: mechanisms of aggregations execution

Query Annotation

To build query clause. It includes Query Context Annotation and Query Type Annotation, and they must be used together.

Query Context Annotation

Decides how Query Type Annotation affect the hits (just filter the hits or affect the score).

Annotation

Function

@Must

set must query

@MustNot

set must_not query

@Should

set should query

@Filter

set filter query

@Or

will be converted to should clause in filter query , like or in sql

@OrNot

will be converted to should.mustNot clause in filter query 中的 should.mustNot, like or not in sql

@Or and @OrNot are not standard query context in Elasticsearch

Query Type Annotation

Decides the behavior of search.

Annotation

Field Type

Function

Parameters

@Match

-

set match query

fieldName: the index field name using in building query, default to the field name that the annotation works on

operator: control boolean clause (or / and)

@MatchPhrase

-

set match phrase query

fieldName: the index field name using in building query, default to the field name that the annotation works on

analyzer: analyzer

slop: term slop

@Term

-

set term query

fieldName: the index field name using in building query, default to the field name that the annotation works on

@Terms

Collection>

set terms query

fieldName: the index field name using in building query, default to the field name that the annotation works on

@Range

Number

set range query

fieldName: the index field name using in building query, default to the field name that the annotation works on

type: boundary type (from / to)

includedBoundary: whether includes boundary

@Exists

Boolean

set exists query

fieldName: the index field name using in building query, default to the field name that the annotation works on

@Wildcard

-

set wildcard query

fieldName: the index field name using in building query, default to the field name that the annotation works on

Method Instruction

AbstractQueryBuilder#build flow chart like this:

934cd17fa5e0c2dd1f0a8ddd9e4055ba.png

You can custom your query in the abstract method.

For example,you can custon filter query like this:

@Override

protected void customFilterQueries(List filterQueries,

StuSearchCriteria stuSearchCriteria) {

// custom query that generated not through annotation

Optional.ofNullable(stuSearchCriteria.getCustomField())

.ifPresent(t -> filterQueries.add(termQuery("customFilterField", t)));

}

You can see more detail usage in the demo under the test/ directory.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值