如何在Elasticsearch上进行联合查询? [英] How to make union query on Elasticsearch?

查看:1652
本文介绍了如何在Elasticsearch上进行联合查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用UNION和限制进行查询。

I would like to make a query with UNION and limit.

我可以在mysql上解释该查询。

I can explain that query on mysql.

(SELECT 
    *
FROM table
WHERE type='text'
LIMIT 3
)
UNION
(SELECT 
    *
FROM table
WHERE type='word'
LIMIT 3
)

我在Elasticsearch上尝试过

I tried it on Elasticsearch

{

    "query":{
        "dis_max":{
            "queries":[
                {
                    "from":0,
                    "size":3,
                    "query":{
                        "match":{
                            "type":"text"
                        }
                    }
                },
                {
                    "from":0,
                    "size":3,
                    "query":{
                        "match":{
                            "type":"word"
                        }
                    }
                }
            ]
        }
    }

}

> http:// localhost :9200 / test / table / _search?pretty& source = {%22query%22:{%22dis_max%22:{%22queries%22:[{%22query%22:{%22match%22:{% 22type%22:%22test%22}}}}}}}
然后,发生了错误。

http://localhost:9200/test/table/_search?pretty&source={%22query%22:{%22dis_max%22:{%22queries%22:[{%22query%22:{%22match%22:{%22type%22:%22test%22}}}]}}} Then, the error occured.

{
  "error" : {
    "root_cause" : [ {
      "type" : "query_parsing_exception",
      "reason" : "[_na] query malformed, no field after start_object",
      "index" : "test",
      "line" : 1,
      "col" : 34
    } ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [ {
      "shard" : 0,
      "index" : "test",
      "node" : "U6yIqY-pS526Vz8QTi6d0Q",
      "reason" : {
        "type" : "query_parsing_exception",
        "reason" : "[_na] query malformed, no field after start_object",
        "index" : "test",
        "line" : 1,
        "col" : 34
      }
    } ]
  },
  "status" : 400
}

当我只使用匹配查询时,它可以工作。

When I use only match query, it worked. But with size, it doesn't work.

我该怎么解决?

推荐答案

方法是多搜索

curl -XGET 'http://127.0.0.1:9200/indexname/_msearch'  -d '
{}
{"query" : {"term" : {"type" : "text"}}, "size" : 3}
{}
{"query" : {"term" : {"type" : "word"}}, "size" : 3}
'

这篇关于如何在Elasticsearch上进行联合查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆