2016-11-17 81 views
1

我正在寻找实现用于搜索和筛选的单个查询。但正如我预料的那样,当我应用过滤时,过滤条件适用于所有类型,所以我只得到那些具有过滤属性和值的文档的结果。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',价值为'VALUE'

我所预料到的是,使用单个查询,获取所有类型的结果(产品,类别,生产商),即满足搜索查询,只有在产品应用过滤。

所以我怀疑是

是否有弹性搜索任何方式对特定类型 搜索结果单独应用过滤不是适用于所有类型的?

回答

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" 
        } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 
+0

谢谢你,一个疑问,是否必须提比过滤器产品其它类型查询?就像你在should块中添加了Category和Manufacturer一样,因为我在请求Uri |中添加了所有内容 POST/my-index /产品,类别,制造商/ _search – LMK

+0

试着不要,但也删除'minimum_should_match'子句,否则它将无法正常工作。 – Val

+0

当然..........(y) – LMK

相关问题