ElasticSearch 使用filter时不对查询关键字做分分词,而query会。

这个可以通过以下实验验证。


DELETE /student
#创建student索引,并指定分词器。这个分词器看上去会同时应用于倒排索引建立时分词和查询关键字分词。不##需要再指定缺省查询分词器:
#        "default_search": {
#          "type": "ik_max_word"
#        }
#但是官方文档有提到可以单独设置缺省查询分词器。
#https://www.elastic.co/guide/en/elasticsearch/reference/current/specify-analyzer.html
 
#创建student索引
PUT student
{
  "settings": {
    "analysis": {
      "analyzer": {
        "default": {
          "type": "ik_max_word"
        }
      }
    }
  }
}
 
#插入数据
 
POST /student/_doc/1
{
  "name":"徐小小",
  "address":"杭州",
  "age":3,
  "interests":"唱歌 画画  跳舞",
  "birthday":"2017-06-19"
}
 
POST /student/_doc/2
{
  "name":"刘德华",
  "address":"香港",
  "age":28,
  "interests":"演戏 旅游 小",
  "birthday":"1980-06-19"
}
 
 
POST /student/_doc/3
{
  "name":"张小斐",
  "address":"北京",
  "age":28,
  "interests":"小品 旅游 小米手机",
  "birthday":"1990-06-19"
}
 
POST /student/_doc/4
{
  "name":"王小宝",
  "address":"德州",
  "age":63,
  "interests":"演戏 小品 打牌 小米电视",
  "birthday":"1956-06-19"
}
 
POST /student/_doc/5
{
  "name":"向华强",
  "address":"香港",
  "age":31,
  "interests":"演戏",
  "birthday":"1958-06-19"
}
 
#通过query查询"小米电器",可以得到正确结果
 
GET student/_search
{
  "query": {
    "match": {
      "interests": "小米"
    }
  }
}
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.6288345,
    "hits": [
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "4",
        "_score": 0.6288345,
        "_source": {
          "name": "王小宝",
          "address": "德州",
          "age": 63,
          "interests": "演戏 小品 打牌 小米电视",
          "birthday": "1956-06-19"
        }
      },
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "张小斐",
          "address": "北京",
          "age": 28,
          "interests": "小品 旅游 小米手机",
          "birthday": "1990-06-19"
        }
      }
    ]
  }

而如果使用filter,则没有任何结果。

GET /student/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
         
          "interests": "小米电器"
        }
      }
    }
  }
}

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

而查询"小米"才有结果

GET /student/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
         
          "interests": "小米"
        }
      }
    }
  }
}

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "4",
        "_score": 0,
        "_source": {
          "name": "王小宝",
          "address": "德州",
          "age": 63,
          "interests": "演戏 小品 打牌 小米电视",
          "birthday": "1956-06-19"
        }
      },
      {
        "_index": "student",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "name": "张小斐",
          "address": "北京",
          "age": 28,
          "interests": "小品 旅游 小米手机",
          "birthday": "1990-06-19"
        }
      }
    ]
  }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值