如何在elasticsearch

时间:2015-12-05 04:23:02

标签: json elasticsearch nosql

以下是我的弹性搜索索引中的两个模拟记录。我的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"
                }
            }
        }   

1 个答案:

答案 0 :(得分:1)

由于您的tags字段属于nested类型,因此您需要使用nested filter进行查询。

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

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