如何查询对象数组作为术语查询的一部分

时间:2017-07-17 16:33:15

标签: elasticsearch nest

我正在使用elasticsearch 5.5.0。

我是我的索引我有弹性的json类型吸引力部分的数据看起来像:

"directions": "Exit the M4 at Junction 1",
      "phoneNumber": "03333212001",
      "website": "https://www.londoneye.com/",
      "postCode": "SE1 7PB",
      "categories": [
        {
          "id": "ce4cf4d0-6ddd-49fd-a8fe-3cbf7be9b61d",
          "name": "Theater"
        },
        {
          "id": "5fa1a3ce-fd5f-450f-92b7-2be6e3d0df90",
          "name": "Family"
        },
        {
          "id": "ed492986-b8a7-43c3-be3d-b17c4055bfa0",
          "name": "Outdoors"
        }
      ],
      "genres": [],
      "featuredImage": "https://www.daysoutguide.co.uk/media/1234/london-eye.jpg",
      "images": [],
      "region": "London",

我的下一个查询如下:

 var query2 = Query<Attraction>.Bool(
            bq => bq.Filter(
                fq => fq.Terms(t => t.Field(f => f.Region).Terms(request.Region.ToLower())),
                fq => fq.Terms(t => t.Field(f => f.Categories).Terms(request.Category.ToLower())))

生成的查询如下所示:

    {
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "region": [
              "london"
            ]
          }
        },
        {
          "terms": {
            "categories": [
              "family"
            ]
          }
        }
      ]
    }
  }
}

没有结果。如果我取出类别位,我得到结果。所以我试图对作为对象数组的类别进行术语过滤。看起来我在做这个查询错了。任何有关如何使其发挥作用的任何提示?

此致

伊斯梅尔

1 个答案:

答案 0 :(得分:3)

您仍然可以使用强类型属性访问:

t.Field(f => f.Categories.First().Name)

NEST的属性推断器读者将阅读.First()并获得categories.name

t.Field(f => f.Categories[0].Name)也适用。