使用查询将Elasticsearch索引中两个字段的值总和作为单个字段

时间:2019-01-31 18:59:15

标签: elasticsearch

ES索引:

df_case = dd.read_csv('s3://../.../df_case.csv',dtpye={'Unnamed: 0': 'float64'})

df_limdata = dd.read_csv('s3://../.../df_limdata.csv',dtpye={'Unnamed: 0': 'float64'})


case = dd.merge(df_limdata, df_case, left_on='sacc_id$',right_on='sacc_id$')
# conversion to pandas
df = case.compute()

这是我的Elasticsearch索引中的文档。我需要获取每个文档的价格和数量的总和,作为查询索引的结果。

1 个答案:

答案 0 :(得分:0)

您可以使用Script Fields来为每次匹配返回脚本评估(基于不同的字段)

尝试以下操作:

GET test1/_search
{
    "query" : {
        "match_all": {}
    },
    "script_fields" : {
        "totalcost" : {
            "script" : {
                "lang": "painless",
                "source": "doc['price'].value * doc['qty'].value"
            }
        },
        "sumpriceandqty" : {
            "script" : {
                "lang": "painless",
                "source": "doc['price'].value + doc['qty'].value"                   
            }
        }
    }
}

编辑:更新的索引名称

我在本地测试了它,它可以正常工作,这是输出:

{
  "took": 67,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test1",
        "_type": "doc",
        "_id": "_xEopmgBbjdm4NNpj-tl",
        "_score": 1,
        "fields": {
          "sumpriceandqty": [
            3.2000000029802322
          ],
          "totalcost": [
            0.6000000089406967
          ]
        }
      }
    ]
  }
}