Elasticsearch QueryBuilder简单查询实现解析

发布时间:2020-08-25 04:00:30 作者:晨曦曙光
来源:脚本之家 阅读:604

elasticsearch中存储的全部文档

Elasticsearch QueryBuilder简单查询实现解析

1.matchAllQuery()

matchAllQuery()方法用来匹配全部文档

public class QueryTest { 
  public static void main(String[] args) { 
    //创建对象,设置集群名称和IP地址 
    ElasticsearchUtils es = new ElasticsearchUtils("im_shan", 
        "localhost"); 
    String indexName = "school";//索引名称 
    String typeName = "student";//类型名称 
    //搜索全部文档 
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); 
    SearchResponse searchResponse = es.searcher(indexName, typeName, 
      queryBuilder); 
    SearchHits hits = searchResponse.getHits(); 
    SearchHit[] searchHits = hits.getHits(); 
    int i = 0; 
    for (SearchHit searchHit : searchHits) { 
      String name = (String) searchHit.getSource().get("name"); 
      String birth = (String) searchHit.getSource().get("birth"); 
      String interest = (String) searchHit.getSource().get("interest"); 
      System.out.println("-------------" + (++i) + "------------"); 
      System.out.println(name); 
      System.out.println(birth); 
      System.out.println(interest); 
    } 
 
  } 
}

控制台将输出全部文档

输出: 
-------------1------------ 
amy 
1995-11-10 
singing dancing 
-------------2------------ 
angle 
1996-05-01 
singing reading 
-------------3------------ 
jackson 
1995-08-01 
basketball music reading 
-------------4------------ 
jack 
1995-03-21 
football music 
-------------5------------ 
tom 
1995-09-11 
reading music

2.matchQuery(String name,Object text)

matchQuery("filedname","value")匹配单个字段,匹配字段名为filedname,值为value的文档

java] view plain copy 
//单个匹配,搜索name为jack的文档 
QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "jack"); 

此时会输出一条记录,因为只有一个name为jack的文档

-------------1------------ 
jack 
1995-03-21 
football music 

3.multiMatchQuery(Object text, String... fieldNames)

多个字段匹配某一个值

[java] view plain copy
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music", 
      "name", "interest");//搜索name中或interest中包含有music的文档(必须与music一致)

控制台输出如下

-------------1------------ 
jack 
1995-03-21 
football music 
-------------2------------ 
tom 
1995-09-11 
reading music 
-------------3------------ 
jackson 
1995-08-01 
basketball music reading

4.wildcardQuery()模糊查询

模糊查询,?匹配单个字符,*匹配多个字符

WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name", 
      "*jack*");//搜索名字中含有jack文档(name中只要包含jack即可) 

 控制台输出如下

-------------1------------ 
jackson 
1995-08-01 
basketball music reading 
-------------2------------ 
jack 
1995-03-21 
football music

5.使用BoolQueryBuilder进行复合查询

使用must

java] view plain copy
//模糊查询 
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( 
      "name", "*jack*");//搜索名字中含有jack的文档 
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( 
      "interest", "*read*");//搜索interest中含有read的文档 
 
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
//name中必须含有jack,interest中必须含有read,相当于and 
boolQueryBuilder.must(queryBuilder1); 
boolQueryBuilder.must(queryBuilder2);

控制台输出如下(name中必须含有jack,interest中必须含有read,只有一条文档匹配):

-------------1------------ 
jackson 
1995-08-01 
basketball music reading 

使用should

WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( 
      "name", "*jack*");//搜索名字中含有jack的文档 
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( 
      "interest", "*read*");//搜索interest中含有read的文档 
 
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
//name中含有jack或者interest含有read,相当于or 
boolQueryBuilder.should(queryBuilder1); 
boolQueryBuilder.should(queryBuilder2);

控制台输出(name中含有jack或者interest中含有read的都将被匹配):

------------1------------ 
jackson 
1995-08-01 
basketball music reading 
-------------2------------ 
angle 
1996-05-01 
singing reading 
-------------3------------ 
jack 
1995-03-21 
football music 
-------------4------------ 
tom 
1995-09-11 
reading music

6.分页

通过from和size参数进行分页。From定义查询结果开始位置,size定义返回的hits(一条hit对应一条记录)最大数量。

Elasticsearch QueryBuilder简单查询实现解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. elasticsearch多级嵌套查询笔记
  2. Elasticsearch Aggregation 笔记

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch querybuilder 简单

上一篇:Angular实现模版驱动表单的自定义校验功能(密码确认为例)

下一篇:python数据分析:关键字提取方式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》