2014-10-06 65 views
0

排除零值,我就是用這個查詢,以獲得最顯著值:從ElasticSearch聚集

keywords = Answer.search( 
    :size => 5, 
    :query => { 
     :match => { 
      :question_id => 32481 
     } 
    }, 
    :aggregations => { 
     :keywords => { 
      :significant_terms => { 
       :field => 'text' 
      } 
     } 
    } 
) 

領域是:文本,但它具有零值,所以答案永遠是:

2.1.2 :135 > keywords.map(&:text) 
=> [nil, nil, nil, nil, nil] 

我試圖添加一個過濾器,爲the documentation建議,但它給了我一個解析錯誤:

keywords = Answer.search( 
    :size => 5, 
    :query => { 
     :match => { 
      :question_id => 32481 
     }, 
     :filtered => { 
      :filter => { 
       :exists => { :field => 'text' } 
      } 
     } 
    }, 
    :aggregations => { 
     :keywords => { 
      :significant_terms => { 
       :field => 'text' 
      } 
     } 
    } 
) 

我試過很多組合,沒有成功。我怎樣才能得到有效的文本答案?

+0

嘗試keywords.map(&:text).compact – 2014-10-06 13:52:36

+1

我相信這不是解決方案。我得到了後者查詢的elasticsearch的解析錯誤。第一個結果的緊湊方法只會返回一個空數組。 – 2014-10-06 13:54:57

回答

1

我相信你ES查詢應該轉換爲這樣的事情:

"size": 5, 
    "query": { 
    "filtered": { 
     "query": { "match": { "question_id" : 32481 } }, 
     "filter": { 
     "exists": { 
      "field": "text" 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "keywords": { 
     "significant_terms": { 
     "field": "text" 
     } 
    } 
    } 

這意味着你的「question_id」「匹配」應括在「過濾」元素。