I’m looking for a way to weights documents higher based on a date field called publicationDate
, but where the weight is lowest if the date field is null, instead of highest.
The full query is a bool query with several should-clauses where I’m trying to add a last clause that will prioritize documents for which the field publicationDate
is newer.
My initial attempt almost works (simplified version):
```
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"multi_match": {
"fields": ["title.standard^1.5", "originalTitle.standard^0.8"],
"query": "something",
"_name": "titles",
"boost": 2,
"type": "cross_fields"
}
},
{
"distance_feature": {
"field": "publicationDate",
"origin": "now",
"pivot": "913d",
"boost": 0.5
}
}
]
}
}
}
```
Except that documents with publicationDate: null
end up on top. I’ve tried several things but none that work, for example changing the last clause to:
```
{
"bool": {
"minimum_should_match": 1,
"boost": 0.5,
"filter": {
"exists": {
"field": "publicationDate"
}
},
"should": {
"distance_feature": {
"field": "publicationDate",
"origin": "now",
"pivot": "913d"
}
}
}
}
```
I’ve also tried must
instead of filter
and shuffling around the clauses and bools. Any ideas?
PS: I would also like to use another date field as a fallback if the first one is null, and again the document to be weighted lowest if they both are null. But the main question is the one above.