Perform a like/contains/match operation on a nested array in Azure cognitive search document

55 Views Asked by At

I have the below data in the indexed documents

[
  {
    "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 **park**ing",
      "indoor pool",
      "continental breakfast"
    ],
    "Address": {
      "StreetAddress": "677 5th Ave",
      "City": "New York",
      "StateProvince": "NY"
    }
  },
  {
    "HotelId": "2",
    "HotelName": "SBS Greenotel",
    "Description": "Ideally located on the main commercial artery of the city in the heart of New York.",
    "Tags": [
      "Free wifi",
      "Paid wifi",
      "on-site **park**ing",
      "podium **park**ing",
      "indoor pool",
      "continental breakfast"
    ],
    "Address": {
      "StreetAddress": "677 5th Ave",
      "City": "New York",
      "StateProvince": "NY"
    }
  }
]

And would like to search the hotels who's tags contains or matches the search phrase "park" (highlighted in above data)

I have tried search.ismatch function but the lambda expression doesn't supports it.

Any help on forming the odata query would be appreciated.

1

There are 1 best solutions below

0
Rishabh Meshram On

One possible solution to this is by including analyzer in your index schema for partial matching of keywords.

I have created an index with below schema:

index_schema = {
    "name": "hotels8",
    "fields": [
        {"name": "HotelId", "type": "Edm.String", "key": True, "searchable": False},
        {"name": "HotelName", "type": "Edm.String", "searchable": True},
        {"name": "Description", "type": "Edm.String", "searchable": True},
        {
            "name": "Tags",
            "type": "Collection(Edm.String)",
            "searchable": True,
            "analyzer": "partial_match_analyzer",
        },
        {
            "name": "Address",
            "type": "Edm.ComplexType",
            "fields": [
                {"name": "StreetAddress", "type": "Edm.String", "searchable": True},
                {"name": "City", "type": "Edm.String", "searchable": True},
                {"name": "StateProvince", "type": "Edm.String", "searchable": True},
            ],
        },
    ],
    "analyzers": [
        {
            "@odata.type": "#Microsoft.Azure.Search.CustomAnalyzer",
            "name": "partial_match_analyzer",
            "tokenizer": "standard_v2",
            "tokenFilters": ["lowercase", "my_edgeNGram"],
        }
    ],
    "tokenFilters": [
        {
            "@odata.type": "#Microsoft.Azure.Search.EdgeNGramTokenFilterV2",
            "name": "my_edgeNGram",
            "minGram": 4,
            "maxGram": 25,
            "side": "front",
        }
    ],
}

And uploaded the below data:

 "value": [
        {
            "@search.action": "upload",
            "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"
            },
        },
        {
            "@search.action": "upload",
            "HotelId": "2",
            "HotelName": "SBS Greenotel",
            "Description": "Ideally located on the main commercial artery of the city in the heart of New York.",
            "Tags": ["Free wifi", "Paid wifi", "on-site parking", "podium parking", "indoor pool", "continental breakfast"],
            "Address": {
                "StreetAddress": "677 5th Ave",
                "City": "New York",
                "StateProvince": "NY"
            },
        },
        {
            "@search.action": "upload",
            "HotelId": "3",
            "HotelName": "SBS Greenotel2",
            "Description": "Ideally located on the main commercial artery of the city in the heart of New York.",
            "Tags": ["Free wifi", "Paid wifi", "indoor pool", "continental breakfast"],
            "Address": {
                "StreetAddress": "677 5th Ave",
                "City": "New York",
                "StateProvince": "NY"
            },
        },
    ]

With analyzer for tag field, I was able get results with search query: "park" enter image description here Note: Please modify the schema according to your requirement.

For more details please refer to this documentation.

And you can also check this thread with similar question.