【问题标题】:How to filter Inner Objects in Elasticsearch?如何在 Elasticsearch 中过滤内部对象?
【发布时间】:2015-07-12 00:43:17
【问题描述】:

我在 Elasticsearch 的文档中有一个 contacts 字段。 contacts 字段中的每个元素本身就是一个对象。我想在contacts 字段上使用termterms 过滤器,以便它匹配contacts.province_idX 的文档。我已尝试将 contacts.province_id 作为搜索字段,但它不起作用。我应该如何过滤这些类型的字段?

"contacts": 
[
  {
     "id": 1,
     "address": "address1",
     "tel": "40 07 13 22",
     "doctor_id": 1,
     "type_id": 1,
     "lng": "51.374720",
     "lat": "35.781986",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  },
  {
     "id": 2,
     "address": "address2",
     "tel": null,
     "doctor_id": 1,
     "type_id": 2,
     "lng": "51.520313",
     "lat": "35.726983",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  },
  {
     "id": 3,
     "address": "address3",
     "tel": null,
     "doctor_id": 1,
     "type_id": 2,
     "lng": "51.456368",
     "lat": "35.797505",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  }
]

编辑:

我试过这个查询:

GET /index_name/type_name/_search
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "contacts.province_id": 8
                }
            }
        }
    }
}

但它返回 3 个结果,我期望 5 个结果。有什么问题?

感谢您的帮助。

【问题讨论】:

  • 数组中的每个元素是不同的文档吗?
  • 我认为它应该按照您的方式工作。我看到的唯一错误是您在查询数据时执行了 GET 而不是 POST。

标签: json search filter elasticsearch lucene


【解决方案1】:

您使用的映射是什么?如果您使用的是嵌套类型,请尝试使用nested filter

    {
         "filter": {
                "nested": {
                                "path": "contacts",
                                "filter": {
                                    "term": {
                                        "contacts.province_id": 3
                                    }
                                }
                            }
                        }
                   }

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    相关资源
    最近更新 更多