2016-11-17 38 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