2017-06-06 85 views
0

我在elasticsearch索引中有一些文檔。下面是示例文件elasticsearch在具有多個值的字段中增強查詢

DOC1

{ 
"feild1":["hi","hello","goodmorning"] 
"feild2":"some string" 
"feild3":{} 
} 

DOC2

{ 
"feild1":["hi","goodmorning"] 
"feild2":"some string" 
"feild3":{} 
} 

文檔3

{ 
"feild1":["hi","hello"] 
"feild2":"some string" 
"feild3":{} 
} 

我想查詢feild1中具有值的 「喜」 和 「你好」 如果兩個如果有任何人存在,那麼該文件應該先來,然後它應該在那之後。例如: : 結果應按照DOC1,DOC3,DOC2的順序排列。我試着用提升查詢。但它不會按照我想要的順序重新調整。這是我正在嘗試的查詢。

{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "match_phrase": { 
         "avail_status": true 
        } 
       }, 
       { 
        "bool": { 
         "should": [ 
          { 
           "constant_score": { 
            "filter": { 
            "terms": { 
            "feild1": [ 
             "hi" 
            ] 
            } 
            }, 
            "boost": 20 
           } 
          }, 
          { 
           "constant_score": { 
            "filter": { 
            "terms": { 
            "feild1": [ 
             "hello" 
            ] 
            } 
            }, 
            "boost": 18 
           } 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 

這是先返回我那些有「hi」的文件,然後是那些有「hello」的文件。提前致謝!

+0

與當前查詢檢查你可以選擇由於在custom_score igonore TDF/IDF中封裝了過濾器,因此doc3和doc1得到了相同的分數。我沒有看到ES把doc3放在doc1上面的觀點。如果你正在施加一個模式,以便在具有較少字段的數組之上放置更多字段的數組,並且提升匹配值,那麼我建議開始查看函數分數。讓我知道,如果這是你的情況 – user3775217

+0

是的,我只是在尋找,文件有很高的匹配分數 –

+0

我張貼功能得分 – user3775217

回答

1

若要爲大於field1的文檔添加額外的提升,您可以將funtion_score腳本得分。

映射

{ 
    "mappings": { 
    "document_type" : { 
     "properties": { 
     "field1" : { 
      "type": "text", 
      "fielddata": true 
     }, 
     "field2" : { 
      "type": "text" 
     }, 
     "field3" : { 
      "type": "text" 
     } 
     } 
    } 
    } 
} 

指數文件

POST custom_score_index1/document_type 

{ 
"feild1":["hi","hello","goodmorning"], 
"feild2":"some string", 
"feild3":{} 
} 

POST custom_score_index1/document_type 

{ 
"feild1":["hi","goodmorning"], 
"feild2":"some string", 
"feild3":{} 
} 

POST custom_score_index1/document_type 

{ 
"feild1":["hi","hello"], 
"feild2":"some string", 
"feild3":{} 
} 

查詢與功能評分,如果你可以添加額外的_score較大尺寸字段1

POST custom_score_index1/document_type/_search 
{ 
    "query": { 
     "function_score": { 
      "query": { 
       "bool": { 
        "must": [{ 
          "match_phrase": { 
           "avail_status": true 
          } 
         }, 
         { 
          "bool": { 
           "should": [{ 
             "constant_score": { 
              "filter": { 
               "terms": { 
                "feild1": [ 
                 "hi" 
                ] 
               } 
              }, 
              "boost": 20 
             } 
            }, 
            { 
             "constant_score": { 
              "filter": { 
               "terms": { 
                "feild1": [ 
                 "hello" 
                ] 
               } 
              }, 
              "boost": 18 
             } 
            } 
           ] 
          } 
         } 
        ] 
       } 
      }, 
      "functions": [{ 
       "script_score": { 
        "script": { 
         "inline": "_score + 10000 * doc['field1'].length" 
        } 
       } 
      }], 
      "score_mode": "sum", 
      "boost_mode": "sum" 
     } 
    } 
} 
相關問題