'Combine multiple queries score in ElasticSearch

I work with ElasticSearch to search for job offers.

I would like to be able to search the information of the offers directly in order to obtain a first score, then refine with the localization of the offer to finally score with the type of contract.

Currently, I do the 3 types of requests within the same function score just by modifying the weights.

But I would like to be able to apply a chronological order, in the following order: Text Search > Localization Search > Contract Type Search

That's the example of the current query i'm doing

{
"query": {
    "bool": {
        "filter": [
            {
                "bool": {
                    "should": [
                        {
                            "nested": {
                                "path": "contract_types",
                                "query": {
                                    "term": {
                                        "contract_types.id": 3
                                    }
                                }
                            }
                        },
                        {
                            "nested": {
                                "path": "contract_types",
                                "query": {
                                    "term": {
                                        "contract_types.id": 2
                                    }
                                }
                            }
                        }
                    ],
                    "must": [
                        {
                            "nested": {
                                "path": "display_on",
                                "query": {
                                    "match": {
                                        "display_on.name": "Yupeek"
                                    }
                                }
                            }
                        },
                        {
                            "geo_distance": {
                                "distance": "60km",
                                "location": {
                                    "lat": 43.517214,
                                    "lon": 1.4980801
                                }
                            }
                        }
                    ],
                    "must_not": [
                        {
                            "term": {
                                "id": 273822
                            }
                        }
                    ],
                    "minimum_should_match": 1
                }
            }
        ],
        "must": [
            {
                "function_score": {
                    "query": {
                        "bool": {
                            "should": [
                                {
                                    "simple_query_string": {
                                        "query": "Automaticien Ingénieur Toulouse",
                                        "fields": [
                                            "title.stop^10",
                                            "description.stop^2"
                                        ],
                                        "boost": 5
                                    }
                                },
                                {
                                    "simple_query_string": {
                                        "query": "Industrie",
                                        "fields": [
                                            "sector.stop^1",
                                            "description.stop^2"
                                        ],
                                        "boost": 1
                                    }
                                },
                                {
                                    "simple_query_string": {
                                        "query": "Agroalimentaire",
                                        "fields": [
                                            "job.stop^5",
                                            "description.stop^2"
                                        ],
                                        "boost": 3
                                    }
                                },
                                {
                                    "distance_feature": {
                                        "field": "location",
                                        "pivot": "25km",
                                        "origin": {
                                            "lat": 43.517214,
                                            "lon": 1.4980801
                                        },
                                        "boost": 5
                                    }
                                },
                                {
                                    "nested": {
                                        "path": "contract_types",
                                        "query": {
                                            "match": {
                                                "contract_types.id": {
                                                    "query": 3,
                                                    "boost": 3
                                                }
                                            }
                                        }
                                    }
                                },
                                {
                                    "nested": {
                                        "path": "contract_types",
                                        "query": {
                                            "match": {
                                                "contract_types.id": {
                                                    "query": 2,
                                                    "boost": 1
                                                }
                                            }
                                        }
                                    }
                                }
                            ],
                            "minimum_should_match": 3
                        }
                    }
                }
            }
        ]
    }
},
"from": 0,
"size": 30,
"explain": true,
"highlight": {
    "fields": {
        "title.stop": {
            "number_of_fragments": 5,
            "fragment_size": 1000
        },
        "sector.stop": {
            "number_of_fragments": 5,
            "fragment_size": 1000
        },
        "job.stop": {
            "number_of_fragments": 5,
            "fragment_size": 1000
        },
        "company.stop": {
            "number_of_fragments": 5,
            "fragment_size": 1000
        },
        "description.stop": {
            "number_of_fragments": 5,
            "fragment_size": 1000
        }
    }
}

}



Solution 1:[1]

Have you considered giving different boosts to each query; i.e giving more boost to the important - and less to the least one. That way scoring will take care of what you are trying to do.

Suggested reading - https://www.elastic.co/blog/how-to-improve-elasticsearch-search-relevance-with-boolean-queries

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nirmal