gpt4 book ai didi

elasticsearch - Elasticsearch 查询中的术语和范围过滤器

转载 作者:行者123 更新时间:2023-11-29 02:54:09 25 4
gpt4 key购买 nike

在我的 eleasticsearch 索引中,我有两个索引如下的文档:

POST dyn-props/item
{
"name": "bar foo",
"properties": [
{
"type": "foo",
"value": 1.45
},

{
"type": "bar",
"value": 256.34
},

{
"type": "foobar",
"value": 43.43
}
]
}

POST dyn-props/item
{
"name": "foo bar",
"properties": [
{
"type": "foo",
"value": 33.34
},

{
"type": "bar",
"value": 22.23
}
]
}

在这个项目类型上,我想查询具有 foo 属性且值大于 10 的项目。我可以使用以下查询过滤具有类型为 foo 的属性的项目的结果:

POST dyn-props/item/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"properties.type": "foo"
}
}
}
}
}

但我不确定如何为应用范围过滤器。有什么想法吗?

编辑:

发出以下查询会得到预期的错误结果:

POST dyn-props/item/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": {
"properties.type": "foo"
}
},

{
"range": {
"properties.value": {
"gte" : 10
}
}
}
]
}
}
}
}
}

结果:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "dyn-props",
"_type": "item",
"_id": "PetPVxwARLOcZqlv28xjpw",
"_score": 1,
"_source": {
"name": "bar foo",
"properties": [
{
"type": "foo",
"value": 1.45
},
{
"type": "bar",
"value": 256.34
},
{
"type": "foobar",
"value": 43.43
}
]
}
},
{
"_index": "dyn-props",
"_type": "item",
"_id": "KqOTXcC9RG6FzPsDDDs8Hw",
"_score": 1,
"_source": {
"name": "foo bar",
"properties": [
{
"type": "foo",
"value": 33.34
},
{
"type": "bar",
"value": 22.23
}
]
}
}
]
}
}

最佳答案

找到了答案。这篇文章很有帮助:ElasticSearch – nested mappings and filters

改变了类型的映射:

PUT dyn-props
{
"mappings": {
"item": {
"properties": {
"name": {
"type": "string"
},
"properties": {
"type": "nested"
}
}
}
}
}

通过将属性设置为 nested type ,我能够维护 typevalue 字段之间的关联。

最后,我能够为此发出嵌套查询:

POST dyn-props/item/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "properties",
"filter": {
"bool": {
"must": [
{
"term": {
"type": "foo"
}
},
{
"range": {
"value": {
"gte": 10
}
}
}
]
}
}
}
}
}
}
}

这让我得到了正确的结果:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dyn-props",
"_type": "item",
"_id": "CzTL4sseR2GVYtvf-0slVQ",
"_score": 1,
"_source": {
"name": "foo bar",
"properties": [
{
"type": "foo",
"value": 33.34
},
{
"type": "bar",
"value": 22.23
}
]
}
}
]
}
}

关于elasticsearch - Elasticsearch 查询中的术语和范围过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25959492/

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