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

 

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