【问题标题】:Elasticsearch search on _source fields are not working_source 字段上的 Elasticsearch 搜索不起作用
【发布时间】:2017-04-03 14:49:00
【问题描述】:

我在elastic-search 5中有一个文档,如下

{
  "_index": "my_index",
  "_type": "json",
  "_id": "document_id",
  "_score": 1,
  "_source": {
    "message": "{\"the_id\": \"custom_id\", \"more\": \"Data\"}",
    "type": "json",
    "the_id": "custom_id",
    "@timestamp": "2017-04-03T13:31:39.995Z",
    "port": 48038,
    "@version": "1",
    "host": "127.0.0.1"
  }
}

当我使用 Kibana 控制台查询 _id 如下时,它工作正常并获取记录

 GET _search
{
  "query": { 
    "bool": { 
      "filter": [ 
        { "term":  { "_id": "document_id" }} 
      ]
    }
  }
}

但如果我查询 _source 级别的字段,在这种情况下是 the_id,没有得到任何结果。

 GET _search
{
  "query": { 
    "bool": { 
      "filter": [ 
        { "term":  { "the_id": "custom_id" }} 
      ]
    }
  }
}

如何确保始终能够查询到 _source 级别。

【问题讨论】:

  • the_id 字段的映射是什么?是text 字段还是keyword 字段?
  • 你试过{ "term": { "_source.the_id": "custom_id" }}
  • 我试过 { "term": { "_source.the_id": "custom_id" }},没用。
  • 我没有创建任何映射。 logstash正在生成数据。基本上它是将消息字段,即json本身转换为_source下的json。
  • @sudhanshu 您能否在第二个查询中将the_id 替换为the_id.keyword 并检查结果?我进一步建议您阅读this

标签: elasticsearch


【解决方案1】:

作为本例中使用的默认映射,elasticsearch 会为您的 the_id 字段创建多字段(the_idthe_id.keyword)。这里the_id 将使用text 类型映射创建,the_id.keyword 将使用keyword 类型映射创建。

由于术语查询与字段的exact value 匹配,因此您必须在查询中提供the_id.keyword

要了解更多信息,请阅读官方文档here中的为什么术语查询与我的文档不匹配?部分

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2018-11-03
    • 2015-03-15
    相关资源
    最近更新 更多