elasticsearch-query-builder, 一款可以基于配置化以及参数绑定的ES语句构造神器

本文介绍了elasticsearch-query-builder,一个用于简化ES DSL查询语句构造的工具,支持配置化和参数绑定。内容涵盖工具的使用、参数说明、示例及未来改进方向。
摘要由CSDN通过智能技术生成

前言

在这里,我想向大家推荐一个我自己开发的项目,也就是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"]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值