Search code examples
azureazure-cognitive-search

Dynamic field mapping in Azure Search


I am creating an index in Azure Search Service and looking for a way to allow unknown fileds to be submitted. My documents are semi structured, meaning I know few fields up front. But I want the flexibility to be able to add documents with additional fields.

for example:

{

    "name":"name1",
    "description":"description"
    "unknown,_simple":"test",
    "unknown_complex": [{
       "male":20,
       "female":30 
    }]
}

In the above example, I know about Name and Description fields so they are added to the index with correct mapping. But unknow_simple and unknown_complex types are not know. Users can submit these when they are creating the documents. Right now Azure Search Rest API is complaining with the following error message

The request is invalid. Details: parameters : A resource without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified

How can I achieve this? Thank you for you help.


Solution

  • Per my understanding , you want to design your index designed as semi structured , "name" and "description" are two fixed fields and with fixed data type. There will be two flexible fields that you are not sure about its data type. In fact, you can define its data type as string.

    Let's say you want to store a json object in that filed, just stringizing your json object so that you can set this string value in that field. Converting this value to json when next time you need it .

    If you want to query certain field of json object in your Dynamic field, you can just use the json string fragment as query key to search , just as below : enter image description here

    Lets assume that "Description" filed is my Dynamic field and its value is a json string , I can use the key fragment to get result I want. But it is not so elegant though .