gpt4 book ai didi

json - 弹性查询嵌套查询

转载 作者:行者123 更新时间:2023-12-03 01:52:51 25 4
gpt4 key购买 nike

步骤1:

在 Elasticsearch 上创建索引
带有以下mapping.json的http://localhost:9200/shop

{
"cloth" :
{
"properties" :
{
"name" : { "type" : "string", "index" : "analyzed" },
"variation" :
{
"type" : "nested",
"properties" :
{
"size" :
{
"type" : "string", "index" : "not_analyzed"
},
"color" :
{
"type" : "string", "index" : "not_analyzed"
}
}
}
}
}
}

GET: http://localhost:9200/shop/_mapping/cloth

HTTP / 1.1 200 OK
内容类型:application / json;字符集= UTF-8
内容长度:518

{“shop”:{“mappings”:{“cloth”:{“properties”:{“cloth”:{“properties”:{“properties”:{“properties”:{“name”:{“properties”: {“index”:{“type”:“string”},“type”:{“type”:“string”}}}},“variation”:{“properties”:{“properties”:{“properties”: {“color”:{“properties”:{“index”:{“type”:“string”},“type”:{“type”:“string”}}},“size”:{“properties”: {“index”:{“type”:“string”},“type”:{“type”:“string”}}}}},“type”:{“type”:“string”}}}}} }},“名称”:{“type”:“string”},“variation”:{“properties”:{“color”:{“type”:“string”},“size”:{“type”: “串”}}}}}}}}

第2步:

在data.json下面插入数据
http://localhost:9200/shop/cloth/?_create
{
"name" : "Test shirt",
"variation" : [
{ "size" : "XXL", "color" : "red" },
{ "size" : "XL", "color" : "black" }
]
}

第三步:

尝试使用给定的query.json搜索

http://localhost:9200/shop/cloth/_search
{
"query" : {
"nested" : {
"path" : "variation",
"query" : {
"bool" : {
"must" : [
{ "term" : { "variation.size" : "XXL" } },
{ "term" : { "variation.color" : "black" } }
]
}
}
}
}
}

遵循以下错误

HTTP / 1.1 400错误请求
内容类型:application / json;字符集= UTF-8
内容长度:519
{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"shop","node":"6U9SA_SDRJKfw1bRxwH8ig","reason":{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}}]},"status":400}

嵌套查询的搜索方式是什么?有什么合适的方法可以将映射文件加载到搜索集群中吗?

最佳答案

我认为您没有使用cloth映射正确创建索引。这样做:

# delete your index first
curl -XDELETE localhost:9200/shop

# create it properly
curl -XPUT localhost:9200/shop -d '{
"mappings": {
"cloth": {
"properties": {
"name": {
"type": "string",
"index": "analyzed"
},
"variation": {
"type": "nested",
"properties": {
"size": {
"type": "string",
"index": "not_analyzed"
},
"color": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}'

关于json - 弹性查询嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39016878/

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