gpt4 book ai didi

elasticsearch - ElasticSearch查询类型特定条件?

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

我正在尝试编写查询来执行以下操作:

例如:CAR索引具有以下映射:

ford
color
model
acura
color
model
toyota
color
model
toyota_type

我想写一个查询来搜索所有文档
color =“blue”和toyota_type =“a” ||仅当type =“toyota”时才使用“c”

棘手的部分是,如果我编写这样的查询,则仅显示TOYOTA类型的结果,但不匹配其他类型的文档:
Looking for: 
("color is blue" AND type is not toyota) OR
("color is blue" AND type IS toyota AND toyota_type = 'a' or 'c')
In other words, search ALL documents;
(1) IF type == toyota (toyota_type == 'a' or 'c' ) AND color == "blue"
(2) IF type != toyota color == "blue"

{
"bool": {
"must": [{
"term": {
"color": "blue"
}
}, {
"bool": {
"should": {
"terms": {
"toyota_type": ["a","c"]
}
}
}
}]
}
}

edit: try this and also didn't work. This returns toyota_type != ('a' or 'c')

{
"query":{
"bool": {
"must": [{
"term": {
"color":
}
}, {
"bool": {
"must_not": [{
"term": {
"color": "red"
}
}]
}
}],
"should":{
"terms":{
"toyota_type": ['a','c']
}
}
}
}
}

最佳答案

您在顶级 bool(boolean) 查询中使用must,这是对Elasticsearch的AND查询。因此,查询可以转换为查找“颜色为蓝色”且“toyota_type为'a'或'c'的所有结果”。但是,根据我的需求,您需要查询以下内容:“color is blue”或“toyota_type是'a'或'c'”。如果这是必要条件,则应该在此查询。

{
"bool": {
"should": [
{
"term": {
"color": "blue"
}
},
{
"terms": {
"toyota_type": ["a","c"]
}
}
]
}
}

对于新的编辑案例,查询如下:
{
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "blue"
}
}
},
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"term": {
"type": {
"value": "toyota"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": {
"value": "toyota"
}
}
},
{
"terms": {
"toyota_type": [
"a",
"c"
]
}
}
]
}
}
]
}
}
]
}
}
}

关于elasticsearch - ElasticSearch查询类型特定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48329089/

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