elasticsearch nested query with match

157 Views Asked by At

I need help implementing a complex search. Below are some pictures to make my question clearer

My Model

Elastic Search Model

As can be seen in the picture, I have a model called company, which contains two objects, products as array and country as object, and products include categories, and the country include provinces.

And My Index

{
"id":1,
"title":"first company",
"products":[
    {
        "id":1,
        "title":"product 1",
        "categories":[
            {
                "id":1,
                "title":"category 1"
            },
            {
                "id":2,
                "title":"category 2"
            }
        ]
    }
],
"country":{
    "id":1,
    "title":"country 1",
    "provinces":[
        {
            "id":1,
            "title":"province 1"
        },
        {
            "id":2,
            "title":"province 2"
        }
    ]
}}

And Final Search Engine With Description Yellow Box elasticseacrh filter search

Elastic Search Mapping

{
"properties": {
"id": {
  "type": "long"
},
"title": {
  "type": "keyword"
},
"products": {
  "type": "nested",
  "properties": {
    "id": {
      "type": "long"
    },
    "title": {
      "type": "keyword"
    },
    "categories": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "title": {
          "type": "keyword"
        }
      }
    }
  }
},
"country": {
  "type": "object",
  "properties": {
    "id": {
      "type": "long"
    },
    "title": {
      "type": "keyword"
    },
    "provinces": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "title": {
          "type": "keyword"
        }
      }
    }
  }
}}}

Question According to the above explanation, when the filter is selected from the left side, it will affect the companies and show the target companies based on the selected search filter.

For example, when the product category category 1 is selected, companies that include category 1 will be listed For example, when we select a province, we see companies in the list that include province 1

Also, by filling the search field above the influence list in the yellow box above, these nested objects will be applied at the same time as the filter.

How can such a query be implemented for Elasticsearch?

0

There are 0 best solutions below