Elasticsearch,将嵌套过滤器与普通过滤器相结合

时间:2015-05-26 14:18:12

标签: elasticsearch

我想出了如何在Elasticsearch中映射和过滤嵌套查询。好极了!但尚未解决的问题是过滤“普通”过滤器和嵌套过滤器。你在这里看到的例子没有给出错误,第二个(嵌套的)过滤器似乎正在工作,但第一个不是。在这个例子中,我希望包含两个过滤器,而不仅仅是一个。我做错了什么?

{
  "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 个答案:

答案 0 :(得分:1)

您有两个名为"filter"的键(在"filtered"中),因此其中一个将被忽略。您可能只需将两个过滤器包装在"bool"中(bool可以根据需要嵌套)。

我无法在不设置测试数据的情况下对其进行测试,但请尝试一下,看看它是否让您更接近:

{
   "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"
               }
            }
         }
      }
   }
}