将此mysql查询转换为弹性搜索

时间:2016-03-10 06:33:18

标签: mysql elasticsearch

我需要将这个mysql查询转换为弹性搜索:

select title, id, slug, is_cinema, release_date
        from media
        where media.is_active = 1
        AND media_type_id IN (1,3)
        and CASE media.media_type_id WHEN 1 THEN is_home = 1 ELSE is_home = 0 END
        and MATCH(title) AGAINST("*foo bar*" IN BOOLEAN MODE)
        ORDER by is_cinema DESC, CASE WHEN title LIKE "foo bar%" THEN 0 ELSE 1 END ASC, CASE media.media_type_id WHEN 3 THEN 0 when 1 then 1 ELSE 3 end ASC, release_date DESC limit 10;

我已经完成了以下操作,我得到了搜索结果。但我需要添加此"CASE media.media_type_id WHEN 1 THEN is_home = 1 ELSE is_home = 0 END"并按照与"ORDER by is_cinema DESC, CASE WHEN title LIKE "foo bar%" THEN 0 ELSE 1 END ASC, CASE media.media_type_id WHEN 3 THEN 0 when 1 then 1 ELSE 3 end ASC, release_date DESC limit 10"完全匹配的顺序进行排序。我使用过前缀但它对我来说也不起作用。

以下是我使用的弹性搜索查询:

GET default/media/_search?pretty
{
    "query":{
      "filtered": {
         "query": {
            "bool": {
              "should": [
                 {
                     "match": {
                        "title": {
                            "query": "foo bar",
                            "boost": 2
                        }
                    }
                 },
                 {
                     "match_phrase": {
                        "title": {
                            "query": "foo bar",
                            "boost": 3
                        }
                    }
                 }
              ]
            }
         },
         "filter": {
             "bool": {
                 "must": [
                     {
                        "term":{
                        "is_active":1
                     }
                    },
                    {
                        "bool": {
                            "should": [
                                    {
                                       "term":{
                                            "media_type_id":1
                                        }
                                    },
                                    {
                                        "term":{
                                         "media_type_id":3
                                    }
                                }
                            ]
                        }
                    }
                  ]
             }
         }
      }
   }

0 个答案:

没有答案