【问题标题】:Elasticsearch How to use a compound query and sorting?Elasticsearch 如何使用复合查询和排序?
【发布时间】:2019-12-19 01:42:51
【问题描述】:

我有两个独立的索引 -
1. 产品
2.currency_rates

产品索引中的文档,有以下详细信息-

{
       "prod_id" : 1,
       "currency" : "USD",
       "price" : 1
}

currency_rates 索引中的文档,具有以下详细信息 -

{
      "id" : 1,
      "USD" : 1,
      "SGD" : 0.72,
      "MYR" : 0.24,
      "INR" : 0.014,
      "EUR" : 1.12
}

我希望实现对产品索引的价格字段的排序,
但是因为产品索引中的每个文档都可能使用不同的货币
我需要先将所有货币兑换成美元,
以及对转换后的结果集进行排序。

例如- 产品 -

[{
    "prod_id": 1,
    "currency": "USD",
    "price": 1

}, {
    "prod_id": 2,
    "currency": "INR",
    "price": 60

}]

currency_rates -

{
    "USD": 1,
    "SGD": 0.72,
    "MYR": 0.24,
    "INR": 0.014,
    "EUR": 1.12
}

以下是我的创作查询 -

GET curency_rates/_search
{
  "query": {
    "match_all": {}
  }
}

PUT /curency_rates/_doc/1
{
  "id":1,
  "USD" : 1,
  "SGD" : 0.72,
  "MYR" : 0.24,
  "INR" : 0.014,
  "EUR" : 1.12
}


PUT /products/_doc/1?pretty
{
    "prod_id":1,
    "currency": "USD",
    "price": 1
}
PUT /products/_doc/2?pretty
{
    "prod_id":2,
    "currency": "INR",
    "price": 60
}

GET products/_search
{
  "query": {
    "match_all": {}
  }
}

发现以下是与我的非常相似的用例,
但我不明白他们是如何在运行时从另一个索引获取转换因子,然后在他们的复合查询中使用它 -
Elastic Search sort preprocessing

我想出了以下查询,
根据上面链接中的答案,我指的是 -

GET products/_search
{
    "query": {
        "function_score": {
            "query": {
                "match_all": {}
            },
            "functions": [{
                "script_score": {
                    "script": {
                        "params": {
                            "USD": 1,
                            "SGD": 0.72,
                            "MYR": 0.24,
                            "INR": 0.014,
                            "EUR": 1.12
                        },
                        "source": "doc['price'].value * params.EUR"
                    }
                }
            }]
        }
    }
}

但我得到了错误的结果 -

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 67.2,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 67.2,
        "_source" : {
          "prod_id" : 2,
          "currency" : "INR",
          "price" : 60
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 2.24,
        "_source" : {
          "prod_id" : 3,
          "currency" : "EUR",
          "price" : 2
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.12,
        "_source" : {
          "prod_id" : 1,
          "currency" : "USD",
          "price" : 1
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.12,
        "_source" : {
          "prod_id" : 5,
          "currency" : "MYR",
          "price" : 1
        }
      }
    ]
  }
}

参考资料 -
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score

https://qbox.io/blog/scoring-using-elasticsearch-scripts-part1

查询 -

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    阅读此example。它们具有同一级别的“boost_mode”和“functions”字段。

    试试:

     "functions": [ 
         { 
             "script_score": { 
                 "script": MathHere, 
                 "params": { 
                     ... 
                 } 
             } 
         }
     ], 
     "boost_mode": "replace" 
    

    希望这有帮助! :D

    【讨论】:

    • 删除升压模式。如果您使用“replace”,查询将使用 script_score。这意味着“match_all”没有被计算”
    • 我从查询中删除了 "boost_mode": "replace" 并运行它,结果仍然相同:/
    • 是的。如果不包括在内,则默认为 boost_mode 中的“乘法”。
    • 试过 - "source": "doc['price'].value * params['EUR']",同样的结果,在 "source": "doc['price']. value * params.doc['currency'].value"
    • 尽量只使用不带括号的“params.EUR”,所以它们都应该乘以欧元
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2018-11-11
    • 2014-08-04
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多