1
votes

I'm doing a simple query, with multiple fields, and trying to apply a decay function based on how many days old the given document is. The following query is my attempt:

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

With the following mapping:

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

Gives the following error:

[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}

1

1 Answers

2
votes

The functions need to go one level higher, just inside the function_score and not inside the query, like this:

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