ElasticSearch NEST - 搜索多种类型,但仅对选定的Type应用过滤器

时间:2016-11-17 13:32:44

标签: elasticsearch nest

我希望实现搜索和过滤的单个查询。但正如我应用过滤时所预期的那样,过滤条件适用于所有类型,因此我只得到那些具有过滤属性和值的文档的结果。

例如,

我在这里搜索了3种类型(产品,类别,制造商)

GET /my-index/Product,Category,Manufacturer/_search
{
    "query": {
        "filtered": {
           "query": {...}, //--> Search a word which present in all types
           "filter": {
                "term": {
                  "ProductField": "VALUE"
               }
           }
        }
    }
}

此处我只获得产品类型的结果,因为产品类型仅包含' ProductField' 等字段,其值为&# 39; VALUE'

我期待的是, 使用单个查询 ,获取所有类型的结果(产品,类别,制造商),满足搜索查询并仅应用过滤产品

所以我怀疑是

  

在弹性搜索中是否有任何方法可以对特定类型应用过滤   单独搜索结果而不是应用于所有类型?

1 个答案:

答案 0 :(得分:2)

是的,您可以使用type查询来实现这一目标。在过滤条件中,我们有一个bool/should子句,可以选择CategoryManufacturer而没有任何其他条件,或Product文档有ProductField: VALUE

POST /my-index/Product,Category,Manufacturer/_search
{
  "query": {
    "filtered": {
      "query": {},
      "filter": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "type": {
                "value": "Category"
              }
            },
            {
              "type": {
                "value": "Manufacturer"
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "type": {
                      "value": "Product"
                    }
                  },
                  {
                    "term": {
                      "ProductField": "VALUE"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}