2016-06-18 78 views
0

我正在做一個簡單的查詢,有多個字段,並試圖應用基於給定文檔多少天的衰變函數。下面的查詢是我的嘗試:Elasticsearch日期衰減函數,Rails

{ 
     query: { 
      function_score:{ 
       query: { 
        multi_match: { 
         query: query, 
         fields: ['name', 'location'] 
        }, 
        functions: [{ 
         gauss: { 
          created_at: { 
           origin: 'now', 
           scale: '1d', 
           offset: '2d', 
           decay: 0.5 
          } 
         } 
        }] 
       } 
      } 
     } 
    } 

用下面的映射:

mappings dynamic: 'false' do 
    indexes :name, analyzer: 'english' 
    indexes :location, analyzer: 'english' 
    indexes :created_at, type: 'date' 
end 

```

提供了以下錯誤: [400] {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"No query registered for [gauss]","index":"people","line":1,"col":143}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query_fetch","grouped":true,"failed_shards":[{"shard":0,"index":"jobs","node":"abcdefgZq1PMsd882foA","reason":{"type":"query_parsing_exception","reason":"No query registered for [gauss]","index":"people","line":1,"col":143}}]},"status":400}

回答

0

functions需要走一個水平較高,只是在function_score內而不在query內,如下所示:

{ 
    query: { 
     function_score:{ 
      functions: [{ 
       gauss: { 
        created_at: { 
         origin: 'now', 
         scale: '1d', 
         offset: '2d', 
         decay: 0.5 
        } 
       } 
      }], 
      query: { 
       multi_match: { 
        query: query, 
        fields: ['name', 'location'] 
       } 
      } 
     } 
    } 
}