本文介绍了弹性搜索与群组和其他条件的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ElasticSearch中的新手。

I am newbie in ElasticSearch.

我们正在将代码从关系数据库移动到ElasticSearch。所以我们正在以ElasticSearch查询格式转换我们的查询。

We are currently moving our code from relational DB to ElasticSearch. So we are converting our queries in ElasticSearch query format.

我正在寻找等同于以下查询的ElasticSearch -

I am looking for ElasticSearch equivalent of below query -

SELECT Color, SUM(ListPrice), SUM(StandardCost)
FROM Production.Product
WHERE Color IS NOT NULL 
    AND ListPrice != 0.00 
    AND Name LIKE 'Mountain%'
GROUP BY Color

有人可以为我提供

提前感谢。

推荐答案

您将有一个产品索引,一个产品键入文档,其映射可能看起来像上面的查询:

You'd have a products index with a product type documents whose mapping could look like this based on your query above:

curl -XPUT localhost:9200/products -d '
{
  "mappings": {
    "product": {
      "properties": {
        "Color": {
          "type": "string"
        },
        "Name": {
          "type": "string"
        },
        "ListPrice": {
          "type": "double"
        },
        "StandardCost": {
          "type": "double"
        }
      }
    }
  }
}'

然后ES相当于您以上给出的SQL的查询将如下所示:

Then the ES query equivalent to the SQL one you gave above would look like this:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "default_field": "Name",
          "query": "Mountain*"
        }
      },
      "filter": {
        "bool": {
          "must_not": [
            {
              "missing": {
                "field": "Color"
              }
            },
            {
              "term": {
                "ListPrice": 0
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "by_color": {
      "terms": {
        "field": "Color"
      },
      "aggs": {
        "total_price": {
          "sum": {
            "field": "ListPrice"
          }
        },
        "total_cost": {
          "sum": {
            "field": "StandardCost"
          }
        }
      }
    }
  }
}

这篇关于弹性搜索与群组和其他条件的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:38