Search code examples
azureazure-cognitive-search

It is possible to have varying data structures in an Azure search index?


Below is some of the data I'm putting into an Azure search index:

enter image description here

I could go with this rigid structure but it needs to support different data types. I could keep adding fields - i.e. Field4, Field5, ... but I wondered if I could have something like a JSON field? So the index could be modelled like below:

[
  {
     "entityId":"dba656d3-f044-4cc0-9930-b5e77e664a8f",
     "entityName":"character",
     "data":{
        "name":"Luke Skywalker",
        "role":"Jedi"
     }
  },
  {
     "entityId":"b37bf987-0978-4fc4-9a51-b02b4a5eed53",
     "entityName":"character",
     "data":{
        "name":"C-3PO",
        "role":"Droid"
     }
  },
  {
     "entityId":"b161b9dc-552b-4744-b2d7-4584a9673669",
     "entityName":"film",
     "data":{
        "name":"A new hope"
     }
  },
  {
     "entityId":"e59acdaf-5bcd-4536-a8e9-4f3502cc7d85",
     "entityName":"film",
     "data":{
        "name":"The Empire Strikes Back"
     }
  },
  {
     "entityId":"00501b4a-5279-41e9-899d-a914ddcc562e",
     "entityName":"vehicle",
     "data":{
        "name":"Sand Crawler",
        "model":"Digger Crawler",
        "manufacturer":"Corellia Mining Corporation"
     }
  },
  {
     "entityId":"fe815cb6-b03c-401e-a871-396f2cd3eaba",
     "entityName":"vehicle",
     "data":{
        "name":"TIE/LN starfighter",
        "model":"win Ion Engine/Ln Starfighter",
        "manufacturer":"Sienar Fleet Systems"
     }
  }

]

I know that I can put JSON in a string field, but that would negatively impact the search matching and also filtering.

Is this possible in Azure search or is there a different way to achieve this kind of requirement?


Solution

  • See the article How to model complex data types. The hotel example data translates nicely to your use-case I believe. If your different entities have different sets of properties you can create a "complex type" similar to the Address or Amenities example below.

    Structural updates

    You can add new sub-fields to a complex field at any time without the need for an index rebuild. For example, adding "ZipCode" to Address or "Amenities" to Rooms is allowed, just like adding a top-level field to an index.

     {
      "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,
        }
        . . .
      ]
    }