elasticsearch中不同字段中的两个过滤器(RANGE)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch中不同字段中的两个过滤器(RANGE)相关的知识,希望对你有一定的参考价值。

我是elasticsarch的初学者,我希望下面的查询可用于两个过滤器,它们具有两个范围的不同字段,但只有第一个范围有效。

此过滤器正常工作:

“范围”:{“ pgrk”:{“ gte”:1,“ lte”:10}}

有人可以告诉我为什么下面的第二个过滤器不起作用吗?

“”应该::{“ range”:{“ url_length”:{“ lte”:100}}]

--------------------------在下面的查询中添加两个过滤器-------------- ------------

 {

    "from" : 0, "size" : 10,

    "sort" : [
            { "pgrk" : {"order" : "desc"} },

             { "url_length" : {"order" : "asc"} }
        ],

        "query": {

    "bool": {
    "must": {

    "multi_match" : {
    "query": "netflix",

    "type": "cross_fields",
    "fields": [ "titulo", "descricao", "url" ],
    "operator": "and"
            }
         },
         "filter": {
         "range" : {"pgrk" : { "gte" : 1, "lte" : 10}    }
          },


    "should" : {
            "range" : {"url_length" : { "lte" : 100 } }
            }

            }
    }


          }
答案

不确定,因为没有提供索引映射和样本文档,但您有什么要求,但是我创建了自己的映射和样本文档以向您展示如何在过滤器上下文中创建多个范围查询。

请发表评论,以便其结果不符合您的要求时可以进行修改。

索引定义

{
    "mappings": {
        "properties": {
            "title": {
                "type": "text"
            },
            "url": {
                "type": "keyword"
            },
            "pgrk": {
                "type": "integer"
            },
            "url_length": {
                "type": "integer"
            }
        }
    }
}

索引样本文档

{
    "title": "netflix",
    "url" : "www.netflix.com", --> this shouldn't match as `pgrk > 10`
    "pgrk": 12,
    "url_length" : 50
}

{
    "title": "Netflix",  --> this should match both filetrs
    "url" : "www.netflix.com",
    "pgrk": 8,
    "url_length" : 50
}

{
    "title": "Netflix", --> this should match both filetrs
    "url" : "www.netflix",
    "pgrk": 5,
    "url_length" : 50
}

{“ title”:“ netflix”,“ url”:“ www.netflix”,“ pgrk”:5“ url_length”:80。->注意pgrk与上一个具有相同的5,并且url_length为diff}

搜索查询

{
    "from": 0,
    "size": 10,
    "sort": [
        {
            "pgrk": {
                "order": "desc"
            }
        },
        {
            "url_length": {
                "order": "asc"
            }
        }
    ],
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "netflix",
                    "type": "cross_fields",
                    "fields": [
                        "title",
                        "url"
                    ],
                    "operator": "and"
                }
            },
            "filter": [ --> note filter array to have multiple range queries in filter context
                {
                    "range": {
                        "pgrk": {
                            "gte": 1,
                            "lte" : 10
                        }
                    }
                },
                {
                    "range": {
                        "url_length": {
                            "lte": 100
                        }
                    }
                }
            ]
        }
    }
}

并且搜索结果仅带来三个文档(甚至2个具有相同的pgrk值]

 "hits": [
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix.com",
                    "pgrk": 8,
                    "url_length": 50
                },
                "sort": [
                    8,
                    50
                ]
            },
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "3",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix",
                    "pgrk": 5,
                    "url_length": 50
                },
                "sort": [
                    5,
                    50
                ]
            },
            {
                "_index": "so_range",
                "_type": "_doc",
                "_id": "4",
                "_score": null,
                "_source": {
                    "title": "netflix",
                    "url": "www.netflix",
                    "pgrk": 5,
                    "url_length": 80
                },
                "sort": [
                    5,
                    80
                ]
            }
        ]

以上是关于elasticsearch中不同字段中的两个过滤器(RANGE)的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch合并高亮字段

ElasticSearch 组合过滤器

Elasticsearch:Keep words token 过滤器

Elasticsearch:Keep words token 过滤器

Elasticsearch 过滤布尔查询

如何通过Elasticsearch 6.x中的动态或未知字段进行聚合