首页 > 解决方案 > 用于在 json 文件中的字段中查找 id 的弹性搜索查询

问题描述

我有一个在 elasticsearch 上编制索引的 json 文件,我需要一个查询来检索“_id_osm”。你能帮我吗?这是我的 json 文件的一行:

{
  "index": {
    "_index": "pariss",
    "_type": "sig",
    "_id": 1
  }
}{
  "fields": {
    "_id_osm": 416747747,
    "_categorie": "",
    "_name": [
      ""
    ],
    "_location": [
      36.1941834,
      5.3595221
    ]
  }
}

标签: elasticsearch

解决方案


根据答案中的评论更新了答案,

如果您store的映射为 true,_id_osm那么您可以使用以下查询来获取字段值。

{
  "stored_fields" : ["_id_osm"],
  "query": {
    "match": {
      "_id": 1
    }
  }
}

上面的调用返回下面的响应,您可以注意到fields响应中包含字段名称和值的部分。

 "hits": [
            {
                "_index": "intqu",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "fields": {
                    "_id_osm": [
                        416747747
                    ]
                }
            }
        ]

如果你没有store默认的 true ,那么使用_source过滤来获取数据。

{
  "_source": [ "_id_osm" ],
  "query": {
    "match": {
      "_id": 1
    }
  }
}

它返回下面的响应,你可以看到_source有数据。

 "hits": [
            {
                "_index": "intqu",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "_id_osm": 416747747
                }
            }
        ]

推荐阅读