elasticsearch Elasticsearch:筛选已筛选嵌套字段的脚本和,elasticsearch,groovy,nested,elasticsearch,Groovy,Nested" /> elasticsearch Elasticsearch:筛选已筛选嵌套字段的脚本和,elasticsearch,groovy,nested,elasticsearch,Groovy,Nested" />

elasticsearch Elasticsearch:筛选已筛选嵌套字段的脚本和

elasticsearch Elasticsearch:筛选已筛选嵌套字段的脚本和,elasticsearch,groovy,nested,elasticsearch,Groovy,Nested,我正在尝试用elasticsearch做一些事情,但在任何地方都找不到答案 已获取嵌套对象产品: "products": { "include_in_root": true, "type": "nested", "properties": { "date": { "format": "strict_date_optional_time||epoch_millis", "type": "date" },

我正在尝试用elasticsearch做一些事情,但在任何地方都找不到答案

已获取嵌套对象产品:

 "products": {
    "include_in_root": true,
    "type": "nested",
    "properties": {
      "date": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      },
      "type": {
        "index": "not_analyzed",
        "type": "string"
      },
      "cat4": {
        "index": "not_analyzed",
        "type": "string"
      },
      "geo": {
        "type": "geo_point"
      },
      "baseprice": {
        "type": "long"
      },
      "cat2": {
        "index": "not_analyzed",
        "type": "string"
      },
      "cat3": {
        "index": "not_analyzed",
        "type": "string"
      },
      "feeltemp": {
        "type": "long"
      },
      "cat1": {
        "index": "not_analyzed",
        "type": "string"
      },
      "price": {
        "type": "double"
      },
      "qty": {
        "index": "not_analyzed",
        "type": "string"
      },
      "name": {
        "index": "not_analyzed",
        "type": "string"
      },
      "weather": {
        "index": "not_analyzed",
        "type": "string"
      },
      "id": {
        "index": "not_analyzed",
        "type": "string"
      },
      "stock": {
        "type": "long"
      },
      "brand": {
        "index": "not_analyzed",
        "type": "string"
      }
    }
  }
例如,我只想在type='cartadd'和cat1=“test”时查询它们

问题,如果使用嵌套筛选器的查询:

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "products",
            "filter": {
              "bool": {
                "must": [
                  {
                    "script": {
                      "script": "sum=0;for(obj in _source.products) {sum = sum + 1 }; sum>=1;"
                    }
                  },
                  {
                    "term": {
                      "products.type": "view"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
这不算什么

如果删除嵌套操作:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "script": {
                "script": "sum=0;for(obj in _source.products) {if(obj.type=='cartadd') {sum = sum + obj.price }}; sum>=2;"
              }
            }
          ]
        }
      }
    }
  }
}
它工作并计算对象。但是我不能再过滤嵌套对象了

您可以看到,我在groovy脚本中添加了一个if条件,但我可以动态添加更多的条件

有人知道我该怎么做吗


非常感谢

您需要将
脚本
过滤器移到
嵌套的
查询之外,因为它在
产品
上运行,后者是父文档的一个字段(不是嵌套的):

此外,一个好主意是在父文档中添加一个名为
nbProducts
的新字段,该字段将包含products对象的数量。这将允许您摆脱该脚本并执行如下简单查询:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "nbProducts": {
              "gte": 1
            }
          }
        },
        {
          "nested": {
            "path": "products",
            "filter": {
              "term": {
                "products.type": "view"
              }
            }
          }
        }
      ]
    }
  }
}

谢谢你的回答。问题是:>在第一个解决方案中,它统计所有产品,但未按type=view进行过滤>在第二个解决方案中,nbProducts不是动态的。我需要添加很多其他过滤器。因此,您的意思是,您只想计算与
产品匹配的产品。type=view
条件?这是内在的,如果您得到一个嵌套的
产品
匹配,那么产品计数必然>=1,因为至少有一个匹配。如果有更多条件,可以将它们添加到
嵌套的
查询中的
bool/filter
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "nbProducts": {
              "gte": 1
            }
          }
        },
        {
          "nested": {
            "path": "products",
            "filter": {
              "term": {
                "products.type": "view"
              }
            }
          }
        }
      ]
    }
  }
}