如何在Elasticsearch上进行联合查询?

时间:2017-01-12 01:54:06

标签: elasticsearch querydsl elasticsearch-query

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

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

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

我在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} }}]}}} 然后,发生了错误。

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

当我只使用匹配查询时,它有效。但是尺寸不合适。

我该如何解决?

1 个答案:

答案 0 :(得分:4)

要走的路是MultiSearch

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