【问题标题】:Elasticsearch does not found an existing document using a the DSLElasticsearch 没有找到使用 DSL 的现有文档
【发布时间】:2016-06-24 15:55:14
【问题描述】:

我不知道为什么,使用URI Search的方式搜索文档是返回正确的文档,但是如果我使用API DSL,则找不到该文档。

重现问题:

在没有创建任何索引的情况下,我插入了这个文档:

curl http://localhost:9299/integrationtest-index/searchable/ID_XXXX2 -d '{ "ref" : "XXXX2", "field1" : "value1" }'

所以索引是使用默认映射自动创建的(类型可搜索):

curl http://localhost:9299/integrationtest-index?pretty
{
  "integrationtest-index" : {
    "aliases" : { },
    "mappings" : {
      "searchable" : {
        "properties" : {
          "field1" : {
            "type" : "string"
          },
          "ref" : {
            "type" : "string"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "field1" : "value1",
        "ref" : "XXXX2",
        "number_of_shards" : "5",
        "creation_date" : "1466780216631",
        "number_of_replicas" : "1",
        "uuid" : "GBj2VF-wQy6JP74AqoIn5g",
        "version" : {
          "created" : "2020099"
        }
      }
    },
    "warmers" : { }
  }
}

此查询返回一个文档:

curl http://localhost:9299/integrationtest-index/searchable/_search?q=ref:XXXX2

但是这个不存在的其他查询响应:

curl -XPOST http://localhost:9299/integrationtest-index/searchable/_search/exists -d '
{
 "query":  {
       "term" : {
         "ref" : "XXXX2"
       }
     }  
 }'

为什么最后一个查询说文档不存在?

环境:

  • 弹性搜索 2.2.0
  • Ubuntu 16.04 LTS
  • OpenJDK 运行时环境(内部版本 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14)

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我每隔几个月就会遇到同样的问题,所以我决定自己做出回应并分享我的愚蠢错误。 默认情况下,elasticsearch 使用 index:analyzed,因此带有 term 的查询没有找到任何文档。 如果您使用URI Search way,则elasticsearch 正在执行query_string 而不是术语查询。

    此查询有效:

    curl -XPOST http://localhost:9299/integrationtest-index/searchable/_search/exists -d '
    {
     "query":  {
           "match" : {
             "ref" : "XXXX2"
           }
         }  
     }'
    

    文档中的更多信息,在Why doesn’t the term query match my document?部分中

    【讨论】:

    • 从技术上讲,它使用的是query_string 查询,而不是match 查询。
    • @pickypg 感谢您的评论。知道正确吗?
    • 足够接近,但您可能还应该更新原始查询本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2017-12-14
    • 2016-11-26
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多