2

I figured out how to map and filter on nested queries in Elasticsearch. Yay! But what isn't working out yet is to filter on both a 'normal' filter and a nested filter. The example you see here doesnt give an error and the second (nested) filter seems to be working, but the first one isn't. In this example I want both filters to be included, not just one. What am I doing wrong?

{
  "size": 100,
  "sort": [],
  "query": {
    "filtered": {
      "query": {
        "match_all": []
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "category.untouched": [
                  "Chargers"
                ]
              }
            }
          ],
          "should": [],
          "must_not": {
            "missing": {
              "field": "model"
            }
          }
        }
      },
      "filter": {
        "nested": {
            "path":"phones",
            "filter":{
                "bool": {
                    "must": [
                        {
                            "term": {
                                "phones.name.untouched":"Galaxy S3 Neo I9301"
                            }
                        }
                    ]
                }
            }
        }
      },
      "strategy": "query_first"
    }
  },
  "aggs": {
    "category.untouched": {
      "terms": {
        "field": "category.untouched"
      }
    },
    "brand.untouched": {
      "terms": {
        "field": "brand.untouched"
      }
    },
    "price_seperate": {
      "histogram": {
        "field": "price_seperate",
        "interval": 10,
        "min_doc_count": 1
      }
    },
    "phones.name.untouched": {
      "nested": {
        "path": "phones"
      },
      "aggs": {
        "phones.name.untouched": {
          "terms": {
            "field": "phones.name.untouched"
          }
        }
      }
    }
  }
}

1 Answer 1

1

You have two keys with the name "filter" (in "filtered"), so one of them is going to get ignored. You probably just need to wrap your two filters in a "bool" (bools can be nested as needed).

I can't test it without setting up some test data, but try this and see if it gets you closer:

{
   "size": 100,
   "sort": [],
   "query": {
      "filtered": {
         "query": {
            "match_all": []
         },
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "category.untouched": [
                           "Chargers"
                        ]
                     }
                  },
                  {
                     "nested": {
                        "path": "phones",
                        "filter": {
                           "term": {
                              "phones.name.untouched": "Galaxy S3 Neo I9301"
                           }
                        }
                     }
                  }
               ],
               "should": [],
               "must_not": {
                  "missing": {
                     "field": "model"
                  }
               }
            }
         },
         "strategy": "query_first"
      }
   },
   "aggs": {
      "category.untouched": {
         "terms": {
            "field": "category.untouched"
         }
      },
      "brand.untouched": {
         "terms": {
            "field": "brand.untouched"
         }
      },
      "price_seperate": {
         "histogram": {
            "field": "price_seperate",
            "interval": 10,
            "min_doc_count": 1
         }
      },
      "phones.name.untouched": {
         "nested": {
            "path": "phones"
         },
         "aggs": {
            "phones.name.untouched": {
               "terms": {
                  "field": "phones.name.untouched"
               }
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.