elasticsearch中不同字段的两个过滤器(RANGE)
Two filters (RANGE) in different fields in elasticsearch
我是 elasticarch 的初学者,我希望下面的查询能够与两个过滤器一起使用,具有两个范围的不同字段,但只有第一个范围有效。
此过滤器工作正常:
"range":{"pgrk":{"gte":1,"lte":10}}
谁能告诉我为什么下面的第二个过滤器不起作用?
"should":{
"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 不同
}
搜索查询
{
"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
]
}
]
@Opster
你好吗?
因为当我在查询中使用 (From / Size) 函数时,它没有 return 我希望 return 的文档。比如我在elasticsearch中有5个文档,所有文档都是按顺序排列的,id 1, id 2, id 3, id 4, id 5.
文档 ID 5 有单词 (car),在查询中我说要从 ID 4 ("from": 4, "size": 1,) 中获取单词 (car)不 return ID 5 它有单词 (car),但在查询中我输入了 doc 5 中存在的信息,所以我应该带上 doc 5,但它没有并说没有找到结果。如果我从 ID 0 ("from": 0, "size": 1,) 开始查询,它会找到 ID 5。似乎 elasticsearch 与 ID 混淆了。你能告诉我 ID 发生了什么变化吗?
下面来自 ID 4 的查询不是 return ID 5,但是如果我从 ID 0 中输入查询,那么它 returns ID 5
{
"from": 4, "size": 1,
"query": {
"multi_match": {
"query": "car",
"type": "cross_fields",
"fields": ["title", "description", "url"],
"operator": "and"
}
}
}
我是 elasticarch 的初学者,我希望下面的查询能够与两个过滤器一起使用,具有两个范围的不同字段,但只有第一个范围有效。
此过滤器工作正常:
"range":{"pgrk":{"gte":1,"lte":10}}
谁能告诉我为什么下面的第二个过滤器不起作用?
"should":{ "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 不同
}
搜索查询
{
"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
]
}
]
@Opster
你好吗?
因为当我在查询中使用 (From / Size) 函数时,它没有 return 我希望 return 的文档。比如我在elasticsearch中有5个文档,所有文档都是按顺序排列的,id 1, id 2, id 3, id 4, id 5.
文档 ID 5 有单词 (car),在查询中我说要从 ID 4 ("from": 4, "size": 1,) 中获取单词 (car)不 return ID 5 它有单词 (car),但在查询中我输入了 doc 5 中存在的信息,所以我应该带上 doc 5,但它没有并说没有找到结果。如果我从 ID 0 ("from": 0, "size": 1,) 开始查询,它会找到 ID 5。似乎 elasticsearch 与 ID 混淆了。你能告诉我 ID 发生了什么变化吗?
下面来自 ID 4 的查询不是 return ID 5,但是如果我从 ID 0 中输入查询,那么它 returns ID 5
{
"from": 4, "size": 1,
"query": {
"multi_match": {
"query": "car",
"type": "cross_fields",
"fields": ["title", "description", "url"],
"operator": "and"
}
}
}