我有一个简单数据的索引,我必须过滤和排序它像这样:
记录是这样的:
{
"name": "Product ABC variant XYZ subvariant JKL",
"date": "2023-01-03T10:34:39+01:00"
}
我在搜索名字,它在哪里:“产品FGH”
1.获取完全匹配的记录(字段名),并按日期(字段日期)对它们进行排序DESC
1.如果在1)中没有找到任何记录,或者如果没有精确匹配,但是有相似的记录,则其余记录按默认分数排序。
有没有可能在一个ElasticSearch请求中完成它?它在整个查询中应该是什么样子?
谢谢
2条答案
按热度按时间g9icjywg1#
你正在寻找的是运行Elasticsearch查询的基础上的条件,这是不可能在一个单一的查询,你需要先火第一个查询,如果它不返回任何命中,你需要火第二个.
a0zr77ik2#
使用script_query,你可以按照自己的意愿进行操作。将日期转换为毫秒,并将其分配给"_score"字段以进行精确匹配。对于非精确匹配,你可以简单地返回_score字段
1.对于精确匹配,将按日期字段desc排序。
1.对于非精确匹配,将按_score字段排序
例如: