gpt4 book ai didi

elasticsearch - Elasticsearch不同领域的两个过滤器(RANGE)

转载 作者:行者123 更新时间:2023-12-03 01:18:54 26 4
gpt4 key购买 nike

我是Elasticsarch的初学者,我希望下面的查询可以与两个具有两个不同字段范围的过滤器一起使用,但只有第一个范围有效。

该过滤器正常工作:

“range”:{“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 - Elasticsearch不同领域的两个过滤器(RANGE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61284322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com