ElasticSearch查询语法

本文详细解读了Elasticsearch中的term、match查询及其区别,以及match查询的分词特性。此外,介绍了bool查询的多种逻辑运算,如must、should和boost,以及如何组合条件实现精确和模糊搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.查询关键字

1.terms、term查询

term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的

GET test_index1/_search
{
  "query": {
    "term": {
      "name": "zhangsan"
    }
  }
}

2.match查询

match query 知道分词器的存在,会对field进行分词操作,然后再查询

GET test_index1/_search
{
  "query": {
    "match": {
      "title":  "my ss"
    }
  }
}

match和term区别可以理解为term是精确查询,这边match模糊查询;

1.match会对my ss分词为两个单词

2.term对认为这是一个单词

3.In匹配查询

GET test_index1/_search
{
    "query":{
        "terms":{
            "name":["张三","王五"]
        }
    }
}

4.多条件查询语法

bool 过滤

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

  • must 多个查询条件的完全匹配,相当于 and。
  • must_not 多个查询条件的相反匹配,相当于 not。
  • should 至少有一个查询条件匹配, 相当于 or。

and查询 name=zhangsan,并且describe=xiangxixinxi

GET test_index1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": "zhangsan"
          }
        },
        {
          "term": {
            "describe": "xiangxixinxi"
          }
        }
      ]
    }
  }
}

or查询 name=zhangsan,或describe=xiangxixinxi1

GET test_index1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name": "zhangsan"
          }
        },
        {
          "term": {
            "describe": "xiangxixinxi1"
          }
        }
      ]
    }
  }
}

5.boost权重查询

es查询无指定排序情况下,分值越大越靠前

GET test_index1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name": {
              "term": "zhangsan",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "name": {
              "term": "lisi",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

name=lisi的条件设置boost=2,权重是条件里最大的,匹配name=lisi的分数会乘以2,排在第一位

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值