-->

what is the supported Model for multi-level Docume

2019-09-19 10:12发布

问题:

I have created few documents in DocumentDb and i am looking for how to search multi-level or parent child objects in single document using Azure search services.

can you any one help me out/any links.

回答1:

Azure Search requires documents to be flattened, so what you will need to do is create a query in DocumentDB to help you do this. To get started, there is a document here, that will give you some information on how to model these more complex data types in Azure Search.

Also, I prefer to do this flattening right in DocumentDB. To do this, User Defined Functions (UDF) are a great way to do this. Here is an example that allows you to pass in an Array and take all the items of type "child" and pass it back as an Array.

function convertToArray (data, child) { 
    var resultArray = [];
    for (var i = 0; i < data.length; i++)
    {
        resultArray.push(data[i][child]); 
    }

    return resultArray;
}

Then within DocumentDB, you would do a query something like this:

SELECT 
c.userName, 
udf.convertToArray(c.addresses, "city") as City
FROM c

So if c.addresses looked like this:

[
    {
      "city": "Toronto",
      "country": "Canada"
    },
    {
      "city": "Seattle",
      "country": "USA"
    }
  ]

The output from the UDF would be:

["Toronto", "Seattle"]

which can then be loaded into the Azure Search in an Collection datatype.