Search code examples
azureazure-cognitive-search

Not able to filter the record from Azure Search based on specific key within indexed json data


I have populated Azure search data using my application and this is what is present in Search Explorer in portal.azure.com.

{
  "@odata.context": "https://demosearch.search.windows.net/indexes('<indexname>')/$metadata#docs(*)",
  "value": [
    {
      "@search.score": 1,
      "id": "31",
      "code": "C001105",
      "title": "Demo Course Title 1",
      "creator": "FILE_UPLOAD",
      "events": [
        {
          "eventId": 97,
          "eventStatus": "PLANNING",
          "evtSession": [
            {
              "postCode": "AB10 1AB",
              "townOrCity": "Aberdeen City,",
              "dates": {
                "from": "2022-08-11T08:00:00Z",
                "to": "2022-08-11T11:00:00Z"
              }
            }
          ]
        }
      ]
    },
    {
      "@search.score": 1,
      "id": "45",
      "code": "C001125",
      "title": "Demo Course Title 2",
      "creator": "FILE_UPLOAD",
      "events": [
        {
          "eventId": 98,
          "eventStatus": "IN_PROGRESS",
          "evtSession": [
            {
              "postCode": "BA10 0AN",
              "townOrCity": "Bruton",
              "dates": {
                "from": "2022-08-11T08:00:00Z",
                "to": "2022-08-11T09:30:00Z"
              }
            }
          ]
        }
      ]
    }
  ],
  "@odata.nextLink": "https://demosearch.search.windows.net/indexes('<indexname>')/docs?api-version=2019-05-06&search=%2A&$skip=50"
}

I'm trying below curl to get data where ["townOrCity": "Aberdeen City,"] from Azure search.

curl --location --request POST 'https://demosearch.search.windows.net/indexes/<indexname>/docs/search?api-version=2019-05-06' \
--header 'api-key: XXXX' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{"count":false,"top":0,"skip":30,"search":"*","orderby":"search.score() desc","filter":"(  events/any(evt:  evt/evtSession/any(session:  search.in(session/townOrCity, '\''Aberdeen City,'\'', '\'','\'')  )  )  )","facets":["events/evtSession/townOrCity,count:10000"],"queryType":"full","searchMode":"any"}'

but I'm not getting expected response and value is coming as empty array :

RESPONSE

{
    "@odata.context": "https://demosearch.search.windows.net/indexes('<indexname>')/$metadata#docs(*)",
    "@search.facets": {
        "events/evtSession/townOrCity": []
    },
    "value": []
}

Please help with the correct payload I should be using to filter out the record with "townOrCity" : "Aberdeen City," OR am I doing something wrong with indexing config or anything ?

"townOrCity" : "Aberdeen City,"

Edit 1:

NOTE: comma mentioned after Aberdeen City causes the issue. If I try same thing witout the comma everything works like a charm. But requirement is to support the comma.

$filter=( events/any(evt: evt/evtSession/any(session: search.in(session/townOrCity, 'Aberdeen City,', ',') ) ) ) 

there is data present in index but still its not applying filter properly, instead giving no record in response.


Solution

  • This works :

    $filter=( events/any(evt: evt/evtSession/any(session: search.in(session/townOrCity, 'Aberdeen City,', '|') ) ) )
    

    There are two overloads of the search.in function:

    search.in(variable, valueList)
    search.in(variable, valueList, delimiters)
    

    Due to delimeters, comma inside my valueList was removed and hence actual value got changed. Apparently its an exact-match so empty response returned.

    https://learn.microsoft.com/en-us/azure/search/search-query-odata-search-in-function