簡體   English   中英

如何強制對Elasticsearch“術語”查詢進行不分析

[英]How to force Elasticsearch “terms” query to be not_analyzed

我想在文檔字段中進行完全匹配的ID。 我已經映射了字段以將它們索引為not_analyzed但是在查詢中似乎每個術語都是tokenizde或至少是小寫的。 如何使查詢也not_analyzed 使用ES 1.4.4、1.5.1和2.0.0

這是一個文檔:

 {
    "_index": "index_1446662629384",
    "_type": "docs",
    "_id": "Cat-129700",
    "_score": 1,
    "_source": {
       "similarids": [
          "Cat-129695",
          "Cat-129699",
          "Cat-129696"
       ],
       "id": "Cat-129700"
    }
 }

這是一個查詢:

{
    "size": 10,
    "query": {
        "bool": {
            "should": [{
                "terms": {
                    "similarids": ["Cat-129695","Cat-129699","Cat-129696"]
                }
            }]
        }
    }
}

上面的查詢不起作用。 如果我從文檔ID中刪除大寫字母和破折號,則可以使用。 由於許多原因,我無法做到這一點。 有沒有一種方法可以使not_analyzed像doc字段一樣不進行分析?

如果我對您的理解正確,則只需在映射中的"similarids"上設置"index":"not_analyzed" 如果您已經正確設置了該設置,那么說明您發布的內容中尚不存在其他事情( "terms"查詢不會對搜索字詞進行任何分析)。 您可能需要檢查映射,以確保它按照您的想法進行設置。

為了測試它,我設置了一個簡單的索引,如下所示:

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "id": {
               "type": "string",
               "index": "not_analyzed"
            },
            "similarids": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

然后添加您的文檔:

PUT /test_index/doc/1
{
   "similarids": [
      "Cat-129695",
      "Cat-129699",
      "Cat-129696"
   ],
   "id": "Cat-129700"
}

而且您的查詢工作正常。

POST /test_index/_search
{
   "size": 10,
   "query": {
      "bool": {
         "should": [
            {
               "terms": {
                  "similarids": [
                     "Cat-129695",
                     "Cat-129699",
                     "Cat-129696"
                  ]
               }
            }
         ]
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.53148466,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.53148466,
            "_source": {
               "similarids": [
                  "Cat-129695",
                  "Cat-129699",
                  "Cat-129696"
               ],
               "id": "Cat-129700"
            }
         }
      ]
   }
}

我在這里使用了ES 2.0,但是使用哪個版本都沒關系。 這是我用來測試的代碼:

http://sense.qbox.io/gist/562ccda28dfaed2717b43739696b88ea861ad690

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM