Elastic Search Nested Query with Nested Object

2019-05-26 14:55发布

问题:

This is the type of data I have stored on my index in elastic search. I have to find Recipes with Main Ingredient Beef(and weight less than 1000) with Ingredients -(chilli powder and weight less than 250),(olive oil & weight less than 300 )and similarly for all other ingredients.

   "Name": "Real beef burritos",
      "Ingredients": [
         {"name": "olive oil",
            "id": 27,
            "weight": 200},
         {"name": "bonion","id": 3,"weight": 300},
         {"name": "garlic",
            "id": 2,
            "weight": 100
         },
         {"name": "chilli powder",
            "id": 35,
            "weight": 150},
           {"name": "coriander",
            "id": 40,
            "weight": 600},
         {"name": "tortillas",
            "id": 41,
            "weight": 700}
      ],"Main_ingredient": {
         "type": "Beef",
         "id": 101,
         "weight": 1000
      }}}

Mapping of the index is

{"final":{"mappings":{"superb":{"properties":{"Cook Time":{"type":"long"},"Ingredients":{"type":"nested","properties":{"id":{"type":"short"},"name":{"type":"string"},"type":{"type":"string"},"weight":{"type":"short"}}},"Main_ingredient":{"properties":{"id":{"type":"long"},"type":{"type":"string"},"weight":{"type":"long"}}},"Name":{"type":"string"},"Prep Time":{"type":"long"},"Servings":{"type":"long"},"Tags":{"type":"string"},"Urls":{"type":"string"},"Views":{"type":"long"}}}}}}

My query is

{
  "query": {
    "bool": {
      "must": [
        { "match": { "Main_ingredient.type": "Beef" }}, 
        {"range":{"Main_ingredient.weight":{"lte":1000}}},
        {
          "nested": {
            "path": "Ingredients", 
            "query": {
              "bool": {
                "must": [ 
                  { "match": { "Ingredients.name": "garlic" }},
                  { "range": { "Ingredients.weight":{"lte":400}     }},
                  { "match": { "Ingredients.name": "chilli powder" }},
                  { "range": { "Ingredients.weight":{"lte":400}     }}
                ]
        }}}}
      ]
}}}

But it gives Null.Can anyone help me out .I think I am not using nested query properly

回答1:

Try this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "Main_ingredient.type": "Beef"
          }
        },
        {
          "range": {
            "Main_ingredient.weight": {
              "lte": 1000
            }
          }
        },
        {
          "nested": {
            "path": "Ingredients",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "Ingredients.name": "garlic"
                    }
                  },
                  {
                    "range": {
                      "Ingredients.weight": {
                        "lte": 400
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "Ingredients",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "Ingredients.name": "chilli powder"
                    }
                  },
                  {
                    "range": {
                      "Ingredients.weight": {
                        "lte": 400
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}