简体   繁体   English

有没有办法用嵌套的 json 在 elasticsearch 中搜索?

[英]Is there a way to search in elasticsearch with nested json?

In elastic search data has been saved in below format:在弹性搜索中,数据已保存为以下格式:

{
'name': 'somename',
'data': '{"age": 25}'
}

How to search like如何搜索喜欢

{"match": {"data.age": 25}}

You need to use the nested data type for your data field and then you can use the example given in the same doc to query the nested fields.您需要为data字段使用嵌套数据类型,然后您可以使用同一文档中给出的示例来查询嵌套字段。

In short, you need to include the nested path in your query if its indexed properly.简而言之,如果索引正确,则需要在查询中包含nested path

"nested": {
      "path": "data",

Adding end to end working example, according to your sample根据您的示例添加端到端工作示例

Index mapping索引映射

{
    "mappings": {
        "properties": {
            "name" : {
                "type" : "text"
            },
            "data": {
                "type": "nested"
            }
        }
    }
}

Index request索引请求

{
    "name": "somename",
    "data": {
        "age": 25
    }
}

search request搜索请求

{
    "query": {
        "nested": {
            "path": "data",
            "query": {
                "match": {
                    "data.age": 25
                }
            }
        }
    }
}

And search result和搜索结果

"hits": [
            {
                "_index": "nested",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "somename",
                    "data": {
                        "age": 25
                    }
                }
            }
        ]

GET /myIndex/_search获取/myIndex/_search

{
  "query": {
    "nested": {
      "path": "data",
      "query": {
          {"match": {"data.age": 25}}
      }
    }
  }
}

See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html for more info.有关详细信息,请参阅https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM