2013-08-29 38 views
1

我在我的產品在幾個和結構如下描述使用情況:景深優先

{ 
    "defaultDescription" : "Default Description", 
    "i18nDescription" : { 
     "pt" : "Descrição Padrão", 
     "de" : "Standard-Beschreibung" 
    } 
} 

現在,我有以下幾點要求:執行下面的列表中搜索優先語言(3種語言)。如果第一種語言不在i18nDescription中,則只使用第二種語言,如果第二種語言不存在,則只使用第三種語言,否則使用第二種語言匹配defaultDescription

我的解決辦法是這樣的:

// suppose request comes with the following languages: en, de, pt 
{ 
    "size":10, 
    "fields" : ["defaultDescription", "i18nDescription.en^50", "i18nDescription.de^20", "i18nDescription.pt^10"], 
    "query": { 
     "multi_match" : { "query" : "default", "fields" : ["description","descriptions.fr-CA"] } 
    } 
} 

但是,這種解決方案將只按優先級排序的語言的結果,我想這樣做:i18nDescription.en:search OR (i18nDescription.de:search AND _empty_:i18nDescription.en) OR (i18nDescription.pt:search AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de) OR (description:search AND _empty_:i18nDescription.pt AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de)

有代表這樣的方式是一個ElasticSearch查詢?

回答

2

玩了一下bool查詢我們可以達到預期的效果。

它基本上需要檢查一個字段是否有文本,其他字段(更重要)是空的,所以它會考慮只是最重要的當前字段。

查詢將類似於:

{ 
    "size":10, 
    "query": { 
     "bool" : { 
      "should" : [ 
       { 
        "bool" : { 
         "must" : [ 
          { "multi_match" : { "fields":["defaultDescription"], "query" : "default" } }, 
          { "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de +_missing_:i18nDescription.pt" } } 
         ] 
        } 
       }, 
       { 
        "bool" : { 
         "must" : [ 
          { "multi_match" : { "fields":["i18nDescription.pt"], "query" : "default" } }, 
          { "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de" } } 
         ] 
        } 
       }, 
       { 
        "bool" : { 
         "must" : [ 
          { "multi_match" : { "fields":["i18nDescription.de"], "query" : "default" } }, 
          { "query_string" : { "query" : "+_missing_:i18nDescription.en" } } 
         ] 
        } 
       }, 
       { 
        "bool" : { 
         "must" : [ 
          { "multi_match" : { "fields":["i18nDescription.en"], "query" : "default" } } 
         ] 
        } 
       } 
      ] 
     } 
    } 
}