locked
Can someone give an example of azure search complex type, when child doc has more than 2 fields RRS feed

  • Question

  • In post: https://docs.microsoft.com/en-us/azure/search/search-howto-complex-data-types, the example given is based on 2 fields in child doc i.e. id and description:

    { "id": "2", "name": "Jen Campbell", "company": "Northwind", "locations": [ { "id": "3", "description": "Northwind Headquarter" }, { "id": "4", "description": "Home Office" }

    What if I have another fields in "location", say id, description and post code:

    { "id": "2", "name": "Jen Campbell", "company": "Northwind", "locations": [ { "id": "3", "description": "Northwind Headquarter",

    "postCode": "123456" }, { "id": "4", "description": "Home Office",

    "postCode": "123457" }

    My query could be based on locationId and description or location Id and postCode. how do I flatten such data.

    Do I need to define to combined field? one for id||description and another for id||postCode? or all can be combined in one field with || separator. 

    Secondly, the post does not explain how you actually query these fields through REST API, what would be my query in such case? 

    Saturday, September 22, 2018 9:45 AM

Answers

  • Bikas,

    You could just add another array. For example:
    [
      {
        "id": "1",
        "name": "John Smith",
        "company": "Adventureworks",
        "locations": [
          {
            "id": "1",
            "description": "Adventureworks Headquarters",
            "postalCode": "11"
          },
          {
            "id": "2",
            "description": "Home Office",
            "postalCode": "22"
          }
        ]
      },
      {
        "id": "2",
        "name": "Jen Campbell",
        "company": "Northwind",
        "locations": [
          {
            "id": "3",
            "description": "Northwind Headquarter",
            "postalCode": "33"
          },
          {
            "id": "4",
            "description": "Home Office",
            "postalCode": "44"
          }
        ]
    }]

    Your index would now look like:


    var index = new Index()
    {
        Name = indexName,
        Fields = new[]
        {
            new Field("id", DataType.String) { IsKey = true },
            new Field("name", DataType.String) { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false },
            new Field("company", DataType.String) { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false },
            new Field("locationsId", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true },
            new Field("locationsDescription", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true },
            new Field("locationsPostalCode", DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true },

        }
    };

    For your second question, I would recommend defining two extra combined fields. One is id and description combined, another is id and postcode combined. You can find sample code that queries these fields here https://github.com/liamca/AzureSearchComplexTypes/blob/master/AzureSearchComplexTypesDemo/Program.cs

    Please let me know if you have additional questions.

    Thanks,

    Matt


    Thursday, September 27, 2018 10:43 PM