search - 如何强制对Elasticsearch “terms”查询进行not_analyzed

标签 search elasticsearch lucene search-engine

我想在doc字段中进行完全匹配的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中删除大写字母和破折号,则可以使用。由于许多原因,我无法做到这一点。有没有办法使docid字段类似not_analyzed

最佳答案

如果我对您的理解正确,则只需在映射中的"index":"not_analyzed"上设置"similarids"。如果您已经正确设置了该设置,那么从您发布的内容来看,还有其他事情是显而易见的( "terms" query不会对您的搜索字词进行任何分析)。您可能需要检查映射,以确保它按照您的想法进行设置。

为了测试它,我设置了一个简单的索引,如下所示:

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

关于search - 如何强制对Elasticsearch “terms”查询进行not_analyzed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530701/

相关文章:

php - 日期范围搜索

c# - 字典<string,dynamic>的NEST映射获取空数组

elasticsearch - Logstash doc_as_upsert在Elasticsearch中交叉索引消除重复

search - 在 Elastic Search 中模拟字段折叠/按字段分组

mysql - 按用户结果排序的可扩展全文搜索

azure - 如何在Azure搜索中通过查询更新字段?

search - 我如何解决 "Googlebot can' 无法访问您的网站”的问题?

node.js - Elasticsearch查询返回具有给定ID的文档

tomcat - 如何在 Windows 的 solr 上安装 Rich Document 补丁?

java - 序列化 Integer 对象的大规模处理