gpt4 book ai didi

Elasticsearch 按嵌套字段过滤

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

我在创建具有多种条件的 Elasticsearch 查询时遇到问题。我的模型看起来像:

data class Product(
@Id
val id: String? = null,
val category: String,
val imagesUrls: List<String>,
@Field(type = FieldType.Double)
val price: Double?,
@Field(type = FieldType.Nested)
val parameters: List<Parameter>?
)

data class Parameter(
val key: String,
val values: List<String>
)
我想通过以下方式查询产品:
  • 类别(例如 cars)
  • 价格(在 2 万美元和 5 万美元之间)
  • 和参数 -> 例如有很多参数的产品,比如 key capacity4L , 5L和第二个参数齿轮传动值manual

  • 我当前的查询如下所示:
    GET data/_search
    {

    "size": 10,
    "query": {
    "bool": {
    "must": [
    {
    "term": {
    "category.keyword": {
    "value": "cars"
    }
    }
    },
    {
    "nested": {
    "path": "parameters",
    "query": {
    "bool": {
    "must": [
    {"term": {
    "parameters.key.keyword": {
    "value": "Capacity"
    }
    }},
    {
    "term": {
    "parameters.key": {
    "value": "4L, 5L"
    }
    }
    }
    ]
    }
    }
    }
    }
    ]
    }
    }
  • 你能告诉我当参数键等于容量时如何过滤产品并检查值列表是否包含其中一个值吗?
  • 如何在一个查询中组合许多此类操作?

  • 示例数据:
    {
    "category":"cars",
    "name":"Ferrari",
    "price":50000,
    "parameters":[
    {
    "key":"capacity",
    "values":"4L"
    },
    {
    "key":"gear transmission",
    "values":"automcatic"
    }
    ]
    }

    最佳答案

    The search query shown below queries the data based on:

    1. category (for example cars)
    2. And parameters -> For example products with many parameters, like key capacity values 4L, 5L and second parameter gear transmissionvalues manual

    添加包含索引数据、映射、搜索查询和搜索结果的工作示例
    索引映射:
    {
    "mappings": {
    "properties": {
    "parameters": {
    "type": "nested"
    }
    }
    }
    }
    索引数据:
    {
    "category":"cars",
    "name":"Ferrari",
    "price":50000,
    "parameters":[
    {
    "key":"gear transmission",
    "values":["4L","5L"]
    },
    {
    "key":"capacity",
    "values":"automcatic"
    }
    ]
    }

    {
    "category":"cars",
    "name":"Ferrari",
    "price":50000,
    "parameters":[
    {
    "key":"capacity",
    "values":["4L","5L"]
    },
    {
    "key":"gear transmission",
    "values":"automcatic"
    }
    ]
    }

    {
    "category":"cars",
    "name":"Ferrari",
    "price":50000,
    "parameters":[
    {
    "key":"capacity",
    "values":"4L"
    },
    {
    "key":"gear transmission",
    "values":"automcatic"
    }
    ]
    }
    查询查询:
    {
    "query": {
    "bool": {
    "must": [
    {
    "term": {
    "category.keyword": {
    "value": "cars"
    }
    }
    },
    {
    "nested": {
    "path": "parameters",
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "parameters.key": "capacity"
    }
    },
    {
    "terms": {
    "parameters.values": [
    "4l",
    "5l"
    ]
    }
    }
    ]
    }
    }
    }
    },
    {
    "nested": {
    "path": "parameters",
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "parameters.key": "gear transmission"
    }
    },
    {
    "match": {
    "parameters.values": "automcatic"
    }
    }
    ]
    }
    }
    }
    }
    ]
    }
    }
    }
    搜索结果:
    "hits": [
    {
    "_index": "bstof",
    "_type": "_doc",
    "_id": "1",
    "_score": 3.9281754,
    "_source": {
    "category": "cars",
    "name": "Ferrari",
    "price": 50000,
    "parameters": [
    {
    "key": "capacity",
    "values": "4L"
    },
    {
    "key": "gear transmission",
    "values": "automcatic"
    }
    ]
    }
    },
    {
    "_index": "bstof",
    "_type": "_doc",
    "_id": "2",
    "_score": 3.9281754,
    "_source": {
    "category": "cars",
    "name": "Ferrari",
    "price": 50000,
    "parameters": [
    {
    "key": "capacity",
    "values": [
    "4L",
    "5L"
    ]
    },
    {
    "key": "gear transmission",
    "values": "automcatic"
    }
    ]
    }
    }
    ]

    关于Elasticsearch 按嵌套字段过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63875373/

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