Elasticsearch-对一个field进行多值全文本搜索

添加测试数据:

POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"title" : "this is java and elasticsearch blog"} }
{ "update": { "_id": "2"} }
{ "doc" : {"title" : "this is java blog"} }
{ "update": { "_id": "3"} }
{ "doc" : {"title" : "this is elasticsearch blog"} }
{ "update": { "_id": "4"} }
{ "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }
{ "update": { "_id": "5"} }
{ "doc" : {"title" : "this is spark blog"} }
  • 搜索title中包含java或者elasticsearch的doc,这个不是之前的term搜索,而是full text的全文检索:
GET /forum/article/_search
{
  "query": {
    "match": {
      "title": "java elasticsearch"
    }
  }
}
  • 如果我们想搜索即包含java也包含elasticsearch的doc,那么我们可以执行:
GET /forum/article/_search
{
  "query": {
    "match": {
      "title": {
        "query": "java elasticsearch",
        "operator": "and"
      }
    }
  }
}
  • 如果我们想搜索包含java,elasticsearch,spark,hadoop中至少3条的结果:
GET /forum/article/_search
{
  "query": {
    "match": {
      "title": {
        "query": "java elasticsearch hadoop spark",
        "minimum_should_match":"75%"
      }
    }
  }
}

如果使用bool可以用以下搜索条件,should默认是可以一个都不匹配:

GET /forum/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "java"
          }
        },
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "match": {
            "title": "hadoop"
          }
        },
        {
          "match": {
            "title": "spark"
          }
        }
      ],
      "minimum_should_match": 3
    }
  }
}

实际上,使用诸如上面的match query进行多值搜索的时候,es底层会自动将这个match query转换成bool的语法

例如:

{
  "query": {
    "match": {
      "title": "java elasticsearch"
    }
  }
}

会转换成:

GET /forum/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": {
              "value": "java"
            }
          }
        },
        {
          "term": {
            "title": {
              "value": "elasticsearch"
            }
          }
        }
      ]
    }
  }
}

需要注意关键的一步是,两者之间唯一不同的是match使用了对搜索参数分词的处理,如果直接使用term将不会对搜索参数进行分词处理

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值