2017-01-17 27 views
0

我有2個索引用戶URL。我想根據索引在不同的字段上運行查詢。如何在elasticsearch中使用multi_match和索引?

USER索引,查詢應該ID現場進行搜索。 但在URL搜索必須執行標題id字段。

POST /_search  
    { 
     "query":{ 
      "indices":[ 
      { 
       "indices":[ 
        "URL" 
       ], 
       "query":{ 
        "multi_match":{ 
         "query":"SMU ", 
         "fields":[ 
         "title", 
         "id" 
         ] 
        } 
       } 
      }, 
      { 
       "indices":[ 
        "USER" 
       ], 
       "query":{ 
        "multi_match":{ 
         "query":"SMU ", 
         "fields":[ 
         "name", 
         "id" 
         ] 
        } 
       } 
      } 
      ] 
     } 
    } 

以上查詢不起作用。需要做些什麼才能使其發揮作用。 如何將multi_match搜索與索引搜索合併?

+0

你使用哪個ES版本?什麼不適用於當前查詢? – Val

+0

目前,我正在使用ES 5.1。示例查詢不起作用。我在尋找這樣的工作。 –

回答

1

indices查詢在ES 5中不推薦使用,但它仍然有效,但您的結構不正確,即您需要將每個indices查詢放在bool/filter子句中。

{ 
    "query": { 
    "bool": { 
     "minimum_should_match": 1, 
     "should": [ 
     { 
      "indices": { 
      "indices": [ 
       "URL" 
      ], 
      "query": { 
       "multi_match": { 
       "query": "SMU ", 
       "fields": [ 
        "title", 
        "id" 
       ] 
       } 
      } 
      } 
     }, 
     { 
      "indices": { 
      "indices": [ 
       "USER" 
      ], 
      "query": { 
       "multi_match": { 
       "query": "SMU ", 
       "fields": [ 
        "name", 
        "id" 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

由於indices查詢已被棄用,新的​​想法是在_index場用一個簡單的term查詢,而不是。試試這個:

{ 
    "query": { 
    "bool": { 
     "minimum_should_match": 1, 
     "should": [ 
     { 
      "bool": { 
      "filter": [ 
       { 
       "term": { 
        "_index": "URL" 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "SMU ", 
        "fields": [ 
        "title", 
        "id" 
        ] 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "filter": [ 
       { 
       "term": { 
        "_index": "USER" 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "SMU ", 
        "fields": [ 
        "name", 
        "id" 
        ] 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 
+0

這和我正在尋找的東西差不多。但在上面的答案中,它試圖匹配「URL」和「USER」。我如何在「網址」或「用戶」中搜索。 –

+0

我已經用'should'取代了'filter',並且你擁有它。 – Val

+0

絕對完美。 –

相關問題