2017-03-02 77 views
0

我有一組文檔src,txtflt字段。我想通過以下方式查詢txt字段:Elasticsearch聚合使用top_hits字段與腳本排序

  1. 集團(bucketize)通過src;
  2. 在每個桶中計算前1個最相關的文檔;
  3. 按照_score * doc.flt的值排序每個存儲桶。

到目前爲止,我已經實現了1和2,但不是3.即使3可能不是很有效率,我仍然希望有這樣的選擇。我的查詢是這樣的:

{ 
    "query" : { 
     'match' : { 
      'text' : { 
       'query' : <some text>, 
       'fuzziness' : 'AUTO', 
       'operator' : 'and' 
      } 
     } 
    }, 
    "aggs": { 
     "by_src": { 
      "terms": { 
       "field": "src", 
       "size" : 10, 
       "order" : {"top_score" : "desc"} 
      }, 
      "aggs": { 
       "top_hits" : { 
        "top_hits" : { 
         "sort": { "_score": { "order": "desc" } }, 
         "size" : 1 
        } 
       }, 
       "top_score": { 
        "max" : { 
         "script" : "_score", 
        } 
       } 
      } 
     } 
    } 
} 

回答

0

我相信它的失敗,因爲你不需要使用_source領域應用排序,以每個桶,只是由字段名應用排序:

{ 
    "query" : { 
    'match' : { 
     'text' : { 
      'query' : <some text>, 
      'fuzziness' : 'AUTO', 
      'operator' : 'and' 
     } 
    } 
}, 
"aggs": { 
    "by_src": { 
     "terms": { 
      "field": "src", 
      "size" : 10, 
      "order" : {"top_score" : "desc"} 
     }, 
     "aggs": { 
      "top_hits" : { 
       "top_hits" : { 
        "sort":[{ 
         "flt": {"order": "desc"} 
        }], 
        "size" : 1 
       } 
      }, 
      "top_score": { 
       "max" : { 
        "script" : "_score", 
       } 
      } 
     } 
    } 
    } 
} 

我假設您的文檔有一個名爲flt的字段,您可以使用該字段進行排序。當然,如果您需要它,也可以將排序更改爲asc

+0

謝謝你的回覆,但我不明白。我的問題是,我提供的查詢首先按每個桶內的分數進行排序,並以排名前1的每個桶爲單位排序。我想要做的不是按最高分排序桶,按最高得分乘以最高得分的某個值(即「_score * doc.flt」)或甚至更復雜的函數對它們排序。 – Tzoiker

+0

好吧,現在就得到它..我從來沒有這樣做過,但我認爲你應該調查腳本:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html –

+0

是的,我嘗試過,但迄今爲止沒有成功。我發現了另一個線程與解決相同的問題(提取和使用top_hit字段)在這裏https://github.com/elastic/elasticsearch/issues/17355 – Tzoiker