【自学elasticsearch7】结合es语法和java的HighLevelClient:match、term和bool

【自学elasticsearch7】结合es语法和java的HighLevelClient:match、term和bool

1.match搜索:一般用来模糊查询

match语句的简单写法:在firstmapping索引下查询查询title中包含“华强”的文档

GET /firstmapping/_search
{
    "query":{
        "match": { "content": "华强"}  
    }
}

match在匹配时会对所查找的关键词进行分词,“华强”会被分成“华”和“强”,索引中所有包含这两个字其中之一的文档都会被查出。

对应的java代码:

 public void matchTest(){
        SearchRequest searchRequest = new SearchRequest("firstmapping");//构建请求头
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//构建请求体
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("content", "华强"); //构建查询参数
       
        searchSourceBuilder.query(queryBuilder);//将查询条件赋给请求体
        searchRequest.source(searchSourceBuilder);//将请求体赋给请求头
        SearchResponse searchResponse = null;
        try{
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        }catch (Exception e){
            e.printStackTrace();
        }
        /*
         * 接下来即可从searchResponse中获取查询结果
         */
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

查询结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.7261486,
    "hits" : [
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "125454",
        "_score" : 1.7261486,
        "_source" : {
          "title" : "华强来买阿瓦达啃大瓜",
          "content" : "此时一个摩托开着华强来买瓜......",
          "score" : "10.0"
        }
      },
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "1224553",
        "_score" : 1.410736,
        "_source" : {
          "title" : "强啊bug",
          "content" : "bug很强",
          "score" : "5.5"
        }
      },
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "12321313",
        "_score" : 1.021733,
        "_source" : {
          "title" : "为什么",
          "content" : "华为是一家通讯公司",
          "score" : "5.5"
        }
      }
    ]
  }
}

2.term搜索:term一般用来进行精确查询,搭配bool查询使用

例子:在firstmapping索引下查询查询title中包含“华强”的文档,

GET /firstmapping/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term":{
            "content": "强"
          }
        },
        {
          "term":{
            "content": "华"
          }
        }
      ]
    }
  }
}

查询既包含“华”,又包含“强”的文档。

java代码:

public void termTest(){
        SearchRequest searchRequest = new SearchRequest("firstmapping");//构建请求头
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//构建请求体
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("content","华"))
                .must(QueryBuilders.termQuery("content","强")); //构建查询参数

        searchSourceBuilder.query(queryBuilder);//将查询条件赋给请求体
        searchRequest.source(searchSourceBuilder);//将请求体赋给请求头
        SearchResponse searchResponse = null;
        try{
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        }catch (Exception e){
            e.printStackTrace();
        }
        /*
         * 接下来即可从searchResponse中获取查询结果
         */
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

查询结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7261486,
    "hits" : [
      {
        "_index" : "firstmapping",
        "_type" : "_doc",
        "_id" : "125454",
        "_score" : 1.7261486,
        "_source" : {
          "title" : "华强来买阿瓦达啃大瓜",
          "content" : "此时一个摩托开着华强来买瓜......",
          "score" : "10.0"
        }
      }
    ]
  }
}

bool查询可以使用must和must_not组成多层过滤条件,查找出最合适的结果。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值