gpt4 book ai didi

elasticsearch - 在ElasticSearch中搜索数组元素

转载 作者:行者123 更新时间:2023-12-03 00:07:44 26 4
gpt4 key购买 nike

我试图在数组上搜索两个或多个值,并仅获取那些与所有单词都匹配的值(和子句)

一些例子:

   { "name" : "Chevrolet",
"value" : [ "gasolina", "alcool", "diesel"]
}
{ "name" : "Fiat",
"value" : [ "eletrica", "alcool"]
}
{ "name" : "Honda",
"value" : [ "diesel", "gasolina"]
}

我的 map
{
"mappings": {
"cars": {
"properties": {
"name": {
"type": "string"
},
"GasType": {
"type": "nested",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
}
}

查询:
{
"query": {
"nested": {
"path": "GasType",
"query": {
"bool": {
"must": [
{ "match": {"GasType.value": "gasolina"}},
{ "match": {"GasType.value": "diesel"}}
]
}
}
}
}
}

我的退货总是空的,如果我更改查询,我将得到所有包含“Gasolina”或“柴油”的内容
我需要那些带有“Gasolina”和“柴油”的

最佳答案

您的测试数据与索引的映射不匹配。在您的测试数据中,我看不到嵌套的字段名称GasType。无论如何,以下对我有用:

DELETE test
PUT test
{
"mappings": {
"cars": {
"properties": {
"name": {
"type": "string"
},
"GasType": {
"type": "nested",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
}
}

POST test/cars/_bulk
{"index":{}}
{"name":"Chevrolet","GasType":{"value":["gasolina","alcool","diesel"]}}
{"index":{}}
{"name":"Fiat","GasType":{"value":["eletrica","alcool"]}}
{"index":{}}
{"name":"Honda","GasType":{"value":["diesel","gasolina"]}}
{"index":{}}
{"name":"Honda","GasType":{"value":["diesel"]}}

GET test/_search
{
"query": {
"nested": {
"path": "GasType",
"query": {
"bool": {
"must": [
{
"match": {
"GasType.value": "gasolina"
}
},
{
"match": {
"GasType.value": "diesel"
}
}
]
}
}
}
}
}

关于elasticsearch - 在ElasticSearch中搜索数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668589/

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