【问题标题】:How to search on complex fields in Azure Cognitive Search如何在 Azure 认知搜索中搜索复杂字段
【发布时间】:2021-10-18 08:31:42
【问题描述】:

考虑以下模型,其中Address 具有City 的嵌套属性

{
  "HotelId": "1",
  "HotelName": "Secret Point Motel",
  "Description": "Ideally located on the main commercial artery of the city in the heart of New York.",
  "Tags": ["Free wifi", "on-site parking", "indoor pool", "continental breakfast"],
  "Address": {
    "StreetAddress": "677 5th Ave",
    "City": "New York",
    "StateProvince": "NY"
  },
  "Rooms": [
    {
      "Description": "Budget Room, 1 Queen Bed (Cityside)",
      "RoomNumber": 1105,
      "BaseRate": 96.99,
    },
    {
      "Description": "Deluxe Room, 2 Double Beds (City View)",
      "Type": "Deluxe Room",
      "BaseRate": 150.99,
    }
    . . .
  ]
}

该模型在 Azure 认知搜索中的索引如下,其中 Address 设置为 Edm.ComplexType

{
  "name": "hotels",
  "fields": [
    { "name": "HotelId", "type": "Edm.String", "key": true, "filterable": true },
    { "name": "HotelName", "type": "Edm.String", "searchable": true, "filterable": false },
    { "name": "Description", "type": "Edm.String", "searchable": true, "analyzer": "en.lucene" },
    { "name": "Address", "type": "Edm.ComplexType",
      "fields": [
        { "name": "StreetAddress", "type": "Edm.String", "filterable": false, "sortable": false, "facetable": false, "searchable": true },
        { "name": "City", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true },
        { "name": "StateProvince", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true }
      ]
    },
    { "name": "Rooms", "type": "Collection(Edm.ComplexType)",
      "fields": [
        { "name": "Description", "type": "Edm.String", "searchable": true, "analyzer": "en.lucene" },
        { "name": "Type", "type": "Edm.String", "searchable": true },
        { "name": "BaseRate", "type": "Edm.Double", "filterable": true, "facetable": true }
      ]
    }
  ]
}

现在我正在尝试使用以下查询搜索 City 等于纽约的数据,但它们都不起作用

city eq 'new york' // return no result
address/city eq 'new york' // return error The property 'address/city' does not exist
address.city eq 'new york' // return error The property 'address.city' does not exist

那么如何在 Azure 认知搜索中的 Edm.ComplexType 上进行搜索?

注意:我正在使用 Azure Dotnet SDK (10.1.0)

【问题讨论】:

    标签: azure azure-cognitive-search


    【解决方案1】:

    正确的语法是在$filter 子句中定义OData 表达式。如果您使用的是 REST API,您的 $filter 子句将是:

    Address/City eq 'New York'
    

    您的代码失败的原因是实际字段路径是Address/City,而您将其指定为address/city。使用正确的字段名称后,您的代码应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2020-11-14
      • 2020-03-27
      • 2021-06-06
      • 2022-01-24
      • 2020-07-20
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多