简体   繁体   English

Elasticsearch 按嵌套字段过滤

[英]Elasticsearch filter by nested fields

I have a problem with creating a query to Elasticsearch with many conditions.我在创建具有多种条件的 Elasticsearch 查询时遇到问题。 My model looks like:我的模型看起来像:

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>
)

I would like to query products by:我想通过以下方式查询产品:

  • category (for example cars )类别(例如cars
  • price (between 20k $ and 50k $)价格(在 20k $ 和 50k $ 之间)
  • and parameters -> For example products with many parameters, like key capacity values 4L , 5L and second parameter gear transmission values manual和参数 -> 例如有很多参数的产品,如关键capacity4L5L和第二参数齿轮传动值manual

My current query looks like this:我当前的查询如下所示:

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"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
  • Could you tell me how to filter the product when parameter key is equal to Capacity and check that the values list contains one of the values?你能告诉我当参数键等于容量时如何过滤产品并检查值列表是否包含其中一个值吗?
  • How to combine many this kind operations in one query?如何在一个查询中组合许多此类操作?

Example data:示例数据:

{
   "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 transmission values manual和参数 -> 例如有很多参数的产品,如关键容量值 4L、5L 和第二参数齿轮传动值手册

Adding a working example with index data, mapping, search query, and search result添加包含索引数据、映射、搜索查询和搜索结果的工作示例

Index Mapping:索引映射:

{
  "mappings": {
    "properties": {
      "parameters": {
        "type": "nested"
      }
    }
  }
}

Index Data:指数数据:

{
   "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"
      }
   ]
}

Search Query:搜索查询:

{
  "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"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Search Result:搜索结果:

"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"
                  }
               ]
            }
         }
      ]

When you need to match any one from a list then you can use terms query instead of term .当您需要匹配列表中的任何一个时,您可以使用terms查询而不是term Update the part in query from:更新查询中的部分:

{
  "term": {
    "parameters.key": {
      "value": "4L, 5L"
    }
  }
}

to below:到下面:

{
  "terms": {
    "parameters.values": {
      "value": [
        "4L",
        "5L"
      ]
    }
  }
}

Note that if parameters.key is analysed field and there exist a keyword sub-field for the same, then use it instead.请注意,如果 parameters.key 是分析字段并且存在相同的关键字子字段,则改用它。 eg parameters.values.keyword例如parameters.values.keyword

You can read more on terms query here .您可以在此处阅读有关术语查询的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM