在 ElasticSearch 查询中保留术语的顺序 [英] Preserving order of terms in ElasticSearch query
问题描述
是否可以在 ElasticSearch 中形成一个保留术语排序的查询?
Is it possible in ElasticSearch to form a query that would preserve the ordering of the terms?
一个简单的例子是使用标准分析器对这些文档进行索引:
A simple example would be having these documents indexed using standard analyzer:
- 你知道搜索
- 你知道搜索
- 了解你的搜索
我可以查询+you +search
,这将返回所有文档,包括第三个.
I could query for +you +search
and this would return me all documents, including the third one.
如果我只想检索具有此特定顺序的术语的文档怎么办?我可以形成一个可以为我做的查询吗?
What if I wanted to only retrieve the documents which have the terms in this specific order? Can I form a query that would do that for me?
考虑到可以通过简单地引用文本来使用短语:"you know"
(检索第 1 和第 2 文档)在我看来应该有一种方法可以保留多个术语的顺序不相邻.
Considering it is possible for phrases by simply quoting the text: "you know"
(retrieve 1st and 2nd docs) it feels to me like there should be a way of preserving the order for multiple terms that aren't adjacent.
在上面的简单示例中,我可以使用邻近搜索,但这不包括更复杂的情况.
In the above simple example I could use proximity searches, but this doesn't cover more complex cases.
推荐答案
你可以使用 span_near
查询,它有一个 in_order
参数.
You could use a span_near
query, it has a in_order
parameter.
{
"query": {
"span_near": {
"clauses": [
{
"span_term": {
"field": "you"
}
},
{
"span_term": {
"field": "search"
}
}
],
"slop": 2,
"in_order": true
}
}
}
这篇关于在 ElasticSearch 查询中保留术语的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!