将Mysql查询转换为弹性搜索查询

时间:2018-04-13 12:19:54

标签: php elasticsearch nosql elasticsearch-5

我正在进行弹性搜索,但不是制作弹性搜索查询的专家。找到我的查询,如果可能的话,转换成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

1 个答案:

答案 0 :(得分:0)

正如eliasah评论的那样,弹性搜索中没有连接操作。

Joining queries

通常,您无法在ES中真正执行加入查询。您可以在同一索引下的文档上建立父/子关系,但这是我不会选择的。我最好的建议是对数据进行非规范化处理,并将每个文档设置为“自包含”。尽可能。在这个具体的例子中,一种可能的解决方案是将计算存储在货币中,最终会得到如下查询:

{
  "_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
}