2017-08-30 65 views
3

所以我有一個查詢來獲取記錄和過濾條件是這樣的彈性的搜索字詞查詢AND條件2個性能依賴於彼此

GET tenantforsneha55/permits/_search/ 
    { 
     "from":0, 
     "size":10, 
     "sort":[ 
     { 
      "permitNumber.keyword":{ 
      "order":"asc" 
      } 
     } 
     ], 
     "query":{ 
     "bool":{ 
      "must":[ 
      { 
       "terms":{ 
       "workClassId":[ 
        "1", 
        "2" 
       ] 
       } 
      }, 
      { 
       "terms":{ 
       "typeId":[ 
        "1", 
        "2" 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 

這顯示了這樣 過濾結果獲取記錄,其中[「1」,「2」]中的typeId和[「1」,「2」]中的classId

但是我想過濾條件是這樣的 typeId = 1 and classId = 1 OR typeId = 2和classId = 2。

是否有你有這個方法嗎?我使用NEST ,,從生成此查詢,將是巨大的,如果你能給我在C#代碼,彈性v 5.5

回答

4

可以使用嵌套像下面shouldmust查詢,

{ 
     "from":0, 
     "size":10, 
     "sort":[ 
     { 
      "permitNumber.keyword":{ 
      "order":"asc" 
      } 
     } 
     ], 
     "query":{ 
     "bool":{ 
      "should":[ 
      { 
       "bool": { 
       "must": [ 
        { 
        "term":{ "workClassId":"1" } 
        }, 
        { 
        "term":{ "typeId":"1" } 
        } 
       ] 
       } 
      }, 
      { 
       "bool": { 
       "must": [ 
        { 
        "term":{ "workClassId":"2" } 
        }, 
        { 
        "term":{ "typeId":"2" } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 

這不是最簡單的方法,但應該這樣做。閱讀更多關於https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query

您也可以使用類似的方式使用過濾器。檢查https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html瞭解更多詳情。

0

嘗試結合的should運營商must

GET tenantforsneha55/permits/_search/ 
    { 
     "from":0, 
     "size":10, 
     "sort":[ 
     { 
      "permitNumber.keyword":{ 
      "order":"asc" 
      } 
     } 
     ], 
     "query":{ 
     "bool":{ 
      "should":[ 
      { 
       "match":{ 
       "workClassId": "1", 
       "typeId": "1" 
       } 
      }, 
      { 
       "match":{ 
       "workClassId": "2", 
       "typeId": "2" 
       } 
      } 
      ] 
     } 
     } 
    } 

我寫在記事本,所以原諒任何語法問題。

彈性搜索相同的文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html