保留ElasticSearch查询中的条款顺序 [英] Preserving order of terms in ElasticSearch query

查看:176
本文介绍了保留ElasticSearch查询中的条款顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一个简单的例子是使用标准分析器对这些文档进行索引:

ElasticSearch中是否可能形成一个保留条款顺序的查询。 / p>


  1. 你知道搜索

  2. 你知道搜索

  3. 知道搜索你

我可以查询 +你+搜索将给我所有的文件,包括第三个。



如果我只想检索具有这个特定顺序的条款的文件怎么办?我可以形成一个对我来说的查询?



考虑到短语可以通过简单的引用文本:你知道 / code>(检索第一和第二个文档)感觉对我来说,应该有一种方式来保持与不相邻的多个条款的顺序。



在上面的简单示例中,我可以使用邻近搜索,但这不包括更复杂的情况。

解决方案

您可以使用一个 span_near 查询,它有一个 in_order 参数。

  {
查询:{
span_near:{
子句:[
{
span_term :{
field:你
}
},
{
span_term:{
field:search
}
}
],
slop:2,
in_order:true
}
}
}


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:

  1. You know for search
  2. You know search
  3. Know search for you

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?

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.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆