前言
在这里,我想向大家推荐一个我自己开发的项目,也就是elasticsearch-query-builder
,这个项目目前在github上已经开源,有兴趣的朋友可以去fork或者star,你的star就是对我最大的鼓励。同时,本项目长期维护和更新,我也接受并且很高兴有小伙伴向本项目pull request,或者协同开发,有兴趣的同学可以给我发邮件。
elasticsearch-query-builder
是一个非常方便构造elasticsearch
(后面简称ES) DSL 查询语句的工具包,在elasticsearch-query-builder
中,我尝试基于配置化的操作去构建ES的查询语句,并且接受外界传入参数,这极大的减少了在Java代码中构建ES查询语句的工作,并同时减少了代码量,使代码更加直观和清晰。基于使ES中DSL构造语句和Java代码分离的思想,elasticsearch-query-builder
诞生了。去GithubFork!
构建
elasticsearch-query-builder
工程一般作为jar包为别的工程提供使用,当然,如果需要基于本项目做二次开发,这都需要将Github上克隆本项目到本地
1
|
git clone https://github.com/xiaowei1118/elasticsearch-query-builder.git
|
在将本项目克隆到本地后,执行mvn package
将本项目打成jar包,或者直接将本项目作为你们自己maven项目的module项目。
elasticsearch-query-builder使用详细说明
elastcisearch-query-builder接受配置文件(特定json格式)或者json格式的字符串配置,配置格式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
{ "index": "user_portrait", "type": "docs", "from": "${from}", "size": "10", "include_source": ["name","age"], //需要哪些字段 "exclude_source": ["sex"], //排除哪些字段 "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" } ] } |
参数说明
# index
index表示ES中的索引或者别名。
# type
type表示ES索引或者别名下的type。
# from
from表示检索文档时的偏移量,相当于关系型数据库里的offset。
# include_source
include_source 搜索结果中包含某些字段,格式为json数组,"include_source": ["name","age"]
。