gpt4 book ai didi

elasticsearch - ElasticSearch简单查询

转载 作者:行者123 更新时间:2023-12-02 22:16:22 26 4
gpt4 key购买 nike

我的ElasticSearch中有这样的结构

{
_index: 'index',
_type: 'product',
_id: '896',
_score: 0,
_source: {
entity_id: '896',
category: [
{
category_id: 2,
is_virtual: 'false'
},
{
category_id: 82,
is_virtual: 'false'
}
]
}
}

我想返回所有具有“82” category_id的“产品”。
{
"query": {
"bool": {
"filter": {
"terms": {
"category.category_id": [
82
]
}
}
}
}
}

该查询给了我0次点击。

什么是正确的方法?

最佳答案

添加工作示例,您需要将category定义为嵌套字段,并通过包含nested路径来修改搜索查询

索引对应

{
"mappings": {
"properties": {
"entity_id": {
"type": "text"
},
"category": {
"type": "nested"
}
}
}
}

为您的文件编制索引
{
"entity_id": "896",
"category": [
{
"category_id": 2,
"is_virtual": false
},
{
"category_id": 82,
"is_virtual": false
}
]
}

正确的搜索查询,请注意,我们正在使用不支持常规过滤器的嵌套查询(因此您的查询会出现错误)
{
"query": {
"nested": {
"path": "category",
"query": {
"bool": {
"must": [
{
"match": {
"category.category_id": 82
}
}
]
}
}
}
}
}

搜索结果调整索引文档
 "hits": [
{
"_index": "complexnested",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"entity_id": "896",
"category": [
{
"category_id": 2,
"is_virtual": false
},
{
"category_id": 82,
"is_virtual": false
}
]
}
}
]

关于elasticsearch - ElasticSearch简单查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62187854/

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