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"
        }
      }
    ]
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章