使用多个字段的数组进行弹性搜索

时间:2015-07-06 09:20:33

标签: querydsl elasticsearch-plugin elasticsearch

我有

形式的数据
{
       "Name": "Bolognaise, tomato, olive and feta tart"            
"Prep Time": 20, 
          "Cook Time": 25,       
"Servings": 4,
 "Ingredients": [
              {
                 "name": "puff pastry",
                 "id": 17,
                 "weight": 100
              },
              {                 "name": "bolognaise",
                 "id": 18,
                 "weight": 150
              },
          {
                 "name": "tomatoes",
                 "id": 19,
                 "weight": 200
              },
              {
                 "name": "olives",
                 "id": 20,
                 "weight": 300
              },
              {
                 "name": "cheese",
                 "id": 21,
                 "weight": 230
              },
              {
                 "name": "baby rocket",
                 "id": 22,
                 "weight": 400
              }
           ],
           "Views": 0,
           "Urls": "xcd.com",
           "Tags": [
              "Tomato",
              "Lunch"
           ],
           "Main_ingredient": {
              "type": "Tomato",
              "id": 101,
              "weight": 500
           }
        }
     },

我正在使用此查询

 {
  "query": {
    "bool": {
      "must": [
        { "match": { "Main_ingredient.type": "Tomato" }}, 
        {"range":{"Main_ingredient.weight":{"lte":1000}}},
        {
          "nested": {
            "path": "Ingredients", 
            "query": {
              "bool": {
                "must": [ 
                  { "match": { "Ingredients.name": "cheese" }},
                  { "range": { "Ingredients.weight":{"lte":400}     }},
                  { "match": { "Ingredients.name": "olives" }},
                  { "range": { "Ingredients.weight":{"lte":400}     }}
                ]
        }}}}
      ]
}}}

我想更改查询,以便如果我通过重量和主要成分,那么它应该返回主要成分和主要成分的重量小于通过重量的ID。现在它返回null

1 个答案:

答案 0 :(得分:1)

因此,如果我正确理解了您的问题,那么您正在寻找关于Main_ingredients.type的term match和一个less than range的权重。然后你多了这些" AND"过滤器。由于您具有分析Main_ingredient.type字段和Ingredients.name字段的映射,因此必须为这些字符串提供小写值。可能只是将映射更改为不分析这些字段。我想你正在寻找类似下面的东西。您显然希望添加任何额外的"和"过滤过滤块以获取您想要匹配的其他成分。

GET rec/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "and": {
                "filters": [
                  {
                    "term": {
                      "main_ingredient.type": "tomato"
                    }
                  },
                  {
                    "range": {
                      "main_ingredient.weight": {
                        "lt": 1000
                      }
                    }
                  }
                ]
              }
            },
            {
              "and": {
                "filters": [
                  {
                    "term": {
                      "ingredients.name": "tomatoes"
                    }
                  },
                  {
                    "range": {
                      "ingredients.weight": {
                        "lt": 300
                      }
                    }
                  }
                ]
              }
            }
          ],
          "_cache": true
        }
      }
    }
  }
}