简体   繁体   English

将Mysql Query转换为Elastic搜索查询

[英]Convert Mysql Query into Elastic search query

i'm working on elastic search but not expert in making elastic search queries. 我正在从事弹性搜索,但不是从事弹性搜索查询的专家。 find my query bellow and if possible to convert into elasticsearch query then take thanks in advance 在下面找到我的查询,并尽可能将其转换为elasticsearch查询,然后在此先感谢

SELECT
  `currency`.`id`                         AS `cur_id`,
  `currency`.`currency_name`              AS `cur_name`,
  `currency`.`currency_code`              AS `cur_code`,
  `currency`.`currency_slug`              AS `cur_slug`,
  `currency`.`logo`                       AS `cur_logo`,
  `currency`.`added_date`                 AS `cur_added_date`,
  `currency`.`mineable_or_not`            AS `mineable_or_not`,
  `currency`.`market_cap`                 AS `cur_market_cap`,
  `currency`.`circulating_supply`         AS `cur_circulating_supply`,
  `currency`.`max_supply`                 AS `cur_max_supply`,
  `currency`.`total_supply`               AS `cur_total_supply`,
  `currency`.`market_cap`                 AS `ng_cur_market_cap`,
  `currency`.`added_date`                 AS `ng_cur_added_date`,
  `currency`.`circulating_supply`         AS `ng_cur_circulating_supply`,
  `calculations`.`volume_1hour`           AS `cal_volume_1hour`,
  `calculations`.`volume_24hour`          AS `cal_volume_24hour`,
  `calculations`.`volume_168hour`         AS `cal_volume_168hour`,
  `calculations`.`volume_720hour`         AS `cal_volume_720hour`,
  `calculations`.`volume_24hour_btc`      AS `cal_volume_24hour_btc`,
  `calculations`.`current_price`          AS `cal_current_price`,
  `calculations`.`percentage_change`      AS `cal_percentage_change_24h`,
  `calculations`.`percentage_change_1h`   AS `cal_percentage_change_1h`,
  `calculations`.`percentage_change_168h` AS `cal_percentage_change_168h`,
  `calculations`.`volume_24hour`          AS `ng_cal_volume_24hour`,
  `calculations`.`current_price`          AS `ng_cal_current_price`
FROM `currency`
  JOIN `calculations` ON `calculations`.`currency_id` = `currency`.`id`
WHERE `calculations`.`update_status` = 1 AND `currency`.`currency_type` != 3 AND `calculations`.`update_status` = 1 AND `currency`.`status` = 1
ORDER BY `market_cap` DESC
LIMIT 100

As eliasah commented, there is no join operation in elastic search. 正如eliasah所说,弹性搜索中没有join操作。

Joining queries 联接查询

In general you can't really perform joining queries in ES. 通常,您实际上无法在ES中执行联接查询。 You can have a parent/child relationship on documents that are under the same index, but that is something I would not opt into. 您可以在具有相同索引的文档上建立父子关系,但是我不愿意这样做。 My best advice is to denormalize your data and have each document as 'self-contained' as possible. 我最好的建议是对数据进行非规范化处理,并使每个文档尽可能“独立”。 In this specific example, one possible solution is to store the calculations inside the currency, you would end up with a query like: 在此特定示例中,一种可能的解决方案是将计算结果存储在货币内部,最终将得到如下查询:

{
  "_source": ["id", "logo", ..., "calculations.volume_1h","calculations.volume_24h",...],
  "query": {
    "bool": {
      "must":[   
        {
          "match":{
            "calculations.update_status":1
          }
        },
        {
          "match":{
            "currency_type":3
          }
        },
        {
          "match":{
            "status":1
          }
        }
      ]     
  },
  "sort" : [
    {
      "market_cap": {
        "order": "desc"
      }
    }
  ]
  "size":100
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM