必须匹配两个不同的术语



我希望在两组数据匹配的情况下过滤结果当我指定"应该"但不是"必须"时,我会受到打击这是我的查询按预期使用的"匹配",但如果我添加一秒钟,我没有命中,但索引中肯定有具有productspercification的记录。

"query": {
"bool": {
  "should": [
    {
      "multi_match": {
        "query": "control*",
        "fields": [
          "name^15",
          "description^5",
          "productCode"
        ]
      }
    }
  ]
}

}

"post_filter": {
"nested": {
  "path": "productSpecification",
  "query": {
    "bool":{
        "must": [
            {
                "match": {
                    "productSpecification.value":"3 years"
                }
            },
            {
                "match": {
                    "productSpecification.value":"Brand"
                }
            }
        ]           
    }        
  }
  }
}

}

只是将我的头撞在桌子上,现在尝试JSON的不同组合,试图使其返回一些值

{
"myindex": {
    "mappings": {
        "product": {
            "properties": {
                "description": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "dispatchTimeInDays": {
                    "type": "integer"
                },
                "height": {
                    "type": "integer"
                },
                "html": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "leadTimeInDays": {
                    "type": "integer"
                },
                "length": {
                    "type": "integer"
                },
                "limitedStock": {
                    "type": "boolean"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "notes": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "price": {
                    "type": "double"
                },
                "productBrandId": {
                    "type": "integer"
                },
                "productCategory": {
                    "properties": {
                        "code": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "fullPath": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "parentProductCategoryId": {
                            "type": "integer"
                        },
                        "productCategoryId": {
                            "type": "integer"
                        }
                    }
                },
                "productCategoryId": {
                    "type": "integer"
                },
                "productCode": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "productId": {
                    "type": "integer"
                },
                "productImage": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "productSpecification": {
                    "type": "nested",
                    "properties": {
                        "description": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "productId": {
                            "type": "long"
                        },
                        "productSpecificationId": {
                            "type": "long"
                        },
                        "specificationId": {
                            "type": "long"
                        },
                        "value": {
                            "type": "keyword"
                        }
                    }
                },
                "productTypeId": {
                    "type": "integer"
                },
                "reviewRating": {
                    "type": "double"
                },
                "reviewRatingCount": {
                    "type": "integer"
                },
                "sellingPriceGroupId": {
                    "type": "integer"
                },
                "stockAvailable": {
                    "type": "integer"
                },
                "taxRateId": {
                    "type": "integer"
                },
                "url": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "weightKg": {
                    "type": "double"
                },
                "width": {
                    "type": "integer"
                }
            }
        }
    }
}

}

这是我期望通过此查询返回的产品的一个示例

{
"_index": "myindex",
"_type": "product",
"_id": "uQEDbGEBfHre1rYmtsWB",
"_score": 141.5985,
"_source": {
    "productId": 14587,
    "name": "Brand Wave Multi Channel Remote Control",
    "productCode": "111",
    "productCategoryId": 17,
    "length": 3,
    "height": 0,
    "productTypeId": 1,
    "url": "brand-wave-multi-channel-remote-control",
    "productBrandId": 3,
    "width": 0,
    "dispatchTimeInDays": 3,
    "leadTimeInDays": 3,
    "stockAvailable": 0,
    "weightKg": 0.001,
    "reviewRatingCount": 0,
    "limitedStock": false,
    "price": 63,
    "productImage": "Wave-Remote-Control.jpg",
    "productCategory": {
        "productCategoryId": 17,
        "name": "Accessories",
        "fullPath": "Accessories",
        "code": "00011"
    },
    "productSpecification": [{
            "productSpecificationId": 852888,
            "productId": 14587,
            "specificationId": 232,
            "name": "Brand",
            "description": "This is the product manufacturer",
            "value": "Brand"
        },
        {
            "productSpecificationId": 852889,
            "productId": 14587,
            "specificationId": 92,
            "name": "Type",
            "value": "Remote control"
        },
        {
            "productSpecificationId": 852891,
            "productId": 14587,
            "specificationId": 10,
            "name": "Guarantee",
            "value": "3 years"
        },
        {
            "productSpecificationId": 852892,
            "productId": 14587,
            "specificationId": 599,
            "name": "Power Voltage",
            "value": "1.5 V"
        },
        {
            "productSpecificationId": 852893,
            "productId": 14587,
            "specificationId": 29,
            "name": "Dimensions",
            "value": "157mm x 38mm x 19mm"
        },
        {
            "productSpecificationId": 852894,
            "productId": 14587,
            "specificationId": 602,
            "name": "Operation Range",
            "value": "Up to 40m"
        },
        {
            "productSpecificationId": 852895,
            "productId": 14587,
            "specificationId": 601,
            "name": "Power Supply",
            "value": "3V DC; 2 x AAA batteries"
        }
    ]
}

}

经过许多修改后,我的查询现在就像

{
"size": 100,

"aggs": {
    "specifications": {
      "nested": {
        "path": "productSpecification"
      },
      "aggs": {
        "groups": {
          "terms": {
            "field": "productSpecification.name"
          },
          "aggs": {
            "attribute": {
              "terms": {
                "field": "productSpecification.value"
              }
            }
          }
        }
      }
    },
    "price_range": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "to": 50
          },
          {
            "from": 50,
            "to": 100
          },
          {
            "from": 100,
            "to": 150
          },
          {
            "from": 150,
            "to": 200
          },
          {
            "from": 200,
            "to": 250
          },
          {
            "from": 250
          }
        ]
      }
    }
  },
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "control*",
            "fields": [
              "name^15",
              "description^5",
              "productCode"
            ]
          }
        }
      ]
    }
  },
  "post_filter": {
    "query":{
    "nested": {
      "path": "productSpecification",
      "query": {
        "bool":{
            "should": [{
                "bool": {
                    "must":[{
                        "term": {
                            "productSpecification.name.keyword": "Brand"
                        }
                    },
                    {
                        "term": {
                            "productSpecification.value": "Brand"
                        }
                    }
                    ]
                }
            },
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "productSpecification.name.keyword": "Guarantee"
                            }
                        },
                        {
                            "term": {
                                "productSpecification.value": "3 years"
                            }
                        }
                    ]
                }
            }
        ]
      }
      }
    }
  }
  }
  }

productspecification.value是一个关键字数据类型。您应该通过术语查询而不是匹配来查询它。然后您不能使用必须必须使用,因为如果文档具有品牌,则价值也不能以3年的价值作为价值。在您的情况下,您将使用,因为是一个或逻辑的操作员

    {
  "query": {
    "nested": {
      "path": "productSpecification",
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "productSpecification.name.keyword": "Brand"
                    }
                  },
                  {
                    "term": {
                      "productSpecification.value": "Brand"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "productSpecification.name.keyword": "Guarantee"
                    }
                  },
                  {
                    "term": {
                      "productSpecification.value": "3 years"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

终于在大量实验/读取后才能正常工作在此处发布,以防其他问题对其他人有用

{
"post_filter": {
    "bool": {
        "filter": [{
                "nested": {
                    "path": "productSpecification",
                    "query": {
                        "bool": {
                            "filter": [{
                                    "term": {
                                        "productSpecification.name": "Brand"
                                    }
                                },
                                {
                                    "terms": {
                                        "productSpecification.value": [
                                            "Brand1"
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "nested": {
                    "path": "productSpecification",
                    "query": {
                        "bool": {
                            "filter": [{
                                    "term": {
                                        "productSpecification.name": "Guarantee"
                                    }
                                },
                                {
                                    "terms": {
                                        "productSpecification.value": [
                                            "3 years"
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

}