【问题标题】:How to query nested structure in elasticsearch如何在elasticsearch中查询嵌套结构
【发布时间】:2016-03-10 03:32:27
【问题描述】:

以下是我的 elasticsearch 索引中的两条模拟记录。我的 ES 中有数百万条记录。我正在尝试查询 ES 以获取所有具有非空/非空“标签”字段的记录。如果一条记录没有标签(如下面的第二条记录),那么我不想从 ES 中提取它。

如果“书”没有嵌套,那么谷歌搜索似乎下面的查询会起作用 -

curl -XGET 'host:port/book_indx/book/_search?' -d '{
    "query" : {"filtered" : {"filter" : {"exists" :{"field" : "_source"}}}}
}'

但是我没有找到查询嵌套结构的解决方案。我尝试了以下没有运气 -

{"query" : {"filtered" : {"filter" : {"exists" :{"field" : "_source.tags"}}}}}

{"query" : {"filtered" : {"filter" : {"exists" :{"field" : "_source":{"tags"}}}}}}

这里非常感谢任何建议!提前致谢。

{
"_shards": {
    "failed": 0,
    "successful": 12,
    "total": 12
},
"hits": {
    "hits": [
        {
            "_id": "book1",
            "_index": "book",
            "_source": {
                "book_name": "How to Get Organized",
                "publication_date": "2014-02-24T16:50:39+0000",
                "tags": [
                    {
                        "category": "self help",
                        "topics": [
                            {
                                "name": "time management",
                                "page": 6198
                            },
                            {
                                "name": "calendar",
                                "page": 10
                            }
                        ],
                        "id": "WEONWOIR234LI",
                    }
                ],
                "last_updated": "2015-11-11T16:28:32.308+0000"
            },
            "_type": "book"
        },
        {
            "_id": "book2",
            "_index": "book",
            "_source": {
                "book_name": "How to Cook",
                "publication_date": "2014-02-24T16:50:39+0000",
                "tags": [],
                "last_updated": "2015-11-11T16:28:32.308+0000"
            },
            "_type": "book"
        }
    ],
    "total": 1
},
"timed_out": false,
"took": 80

}

映射-

        "book": {
            "_id": {
                "path": "message_id"
            },
            "properties": {
                "book_name": {
                    "index": "not_analyzed",
                    "type": "string"
                },
                "publication_date": {
                    "format": "date_time||date_time_no_millis",
                    "type": "date"
                },
                "tags": {
                    "properties": {
                        "category": {
                            "index": "not_analyzed",
                            "type": "string"
                        },
                        "topic": {
                            "properties": {
                                "name": {
                                    "index": "not_analyzed",
                                    "type": "string"
                                },
                                "page": {
                                    "index": "no",
                                    "type": "integer"
                                }                     
                            }
                        },
                        "id": {
                            "index": "not_analyzed",
                            "type": "string"
                        }
                    },
                    "type": "nested"
                },
                "last_updated": {
                    "format": "date_time||date_time_no_millis",
                    "type": "date"
                }
            }
        }   

【问题讨论】:

  • 您能否分享一下book 类型的映射? tags 字段是 nested 字段还是普通的 object 字段?我也很惊讶在您的文档中没有看到_source
  • @Val 感谢您指出缺少 _source - 我不小心重命名了它。进行了上述更新并包含了映射文件

标签: json elasticsearch nosql


【解决方案1】:

由于您的tags 字段具有nested 类型,因此您需要使用nested filter 来查询它。

以下过滤后的查询将仅正确返回上面的第一个文档(即 id book1

{
  "query": {
    "filtered": {
      "filter": {
        "nested": {
          "path": "tags",
          "filter": {
            "exists": {
              "field": "tags"
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • 感谢您的建议。效果很好! (对延迟回复表示歉意)
  • 不用担心,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多