如何在elasticsearch查询中获得3个随机搜索结果
How to get 3 random search results in elasticserch query
我的 elasticsearch 查询 returns 记录在 publishedDates 范围内:
{
query : {
bool: {
filter: [
],
must: {
range: {
publishedDate: {
gte: "2018-11-01",
lte: "2019-03-30"
}
}
}
}
}
from: 0,
size: 3,
}
我每次发送此查询时都需要显示 3 个随机结果
elastic search documentation中提到我可以发送种子来获得随机结果:
按照文档进行操作后,我将查询更新为:
{
"query" : {
"bool": {
"filter": [
],
"must": {
"range": {
"publishedDate": {
"gte": "2018-11-01",
"lte": "2019-03-30"
}
}
}
},
"function_score": {
"functions": [
{
"random_score": {
"seed": "123123123"
}
}
]
}
},
"from": 0,
"size": 3
}
但它不起作用(说查询格式错误),谁能建议如何将此查询更正为 return 3 个随机搜索结果。
如果您只需要返回随机结果,您可以将查询重组为类似于以下内容
{
"query": {
"function_score": {
"query": {
"range": {
"publishedDate": {
"gte": "2018-11-01",
"lte": "2019-03-30"
}
}
},
"boost": "5",
"random_score": {},
"boost_mode": "multiply"
}
},
"from": 0,
"size": 3
}
修改自 elastic 文档 -
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
我的 elasticsearch 查询 returns 记录在 publishedDates 范围内:
{
query : {
bool: {
filter: [
],
must: {
range: {
publishedDate: {
gte: "2018-11-01",
lte: "2019-03-30"
}
}
}
}
}
from: 0,
size: 3,
}
我每次发送此查询时都需要显示 3 个随机结果
elastic search documentation中提到我可以发送种子来获得随机结果:
按照文档进行操作后,我将查询更新为:
{
"query" : {
"bool": {
"filter": [
],
"must": {
"range": {
"publishedDate": {
"gte": "2018-11-01",
"lte": "2019-03-30"
}
}
}
},
"function_score": {
"functions": [
{
"random_score": {
"seed": "123123123"
}
}
]
}
},
"from": 0,
"size": 3
}
但它不起作用(说查询格式错误),谁能建议如何将此查询更正为 return 3 个随机搜索结果。
如果您只需要返回随机结果,您可以将查询重组为类似于以下内容
{
"query": {
"function_score": {
"query": {
"range": {
"publishedDate": {
"gte": "2018-11-01",
"lte": "2019-03-30"
}
}
},
"boost": "5",
"random_score": {},
"boost_mode": "multiply"
}
},
"from": 0,
"size": 3
}
修改自 elastic 文档 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html