【问题标题】:How to make union query on Elasticsearch?如何在 Elasticsearch 上进行联合查询?
【发布时间】:2017-01-12 01:54:05
【问题描述】:

我想用 UNION 和 limit 进行查询。

我可以在 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
}

当我只使用匹配查询时,它起作用了。但是对于大小,它不起作用。

我该如何解决?

【问题讨论】:

    标签: elasticsearch querydsl elasticsearch-query


    【解决方案1】:

    要走的路是MultiSearch

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

    【讨论】:

    • 谢谢,它适用于 curl。但是如何在 URI Search 上使用它呢?
    • @USER 为什么要在 URI 搜索中这样做?
    • 或者,如何将其发送到 ajax 参数?
    • @USER 尝试发送 POST 请求并查询字符串正文,但我很奇怪您直接向 ES 发送请求,因为每个正文都可能弄乱您的索引
    猜你喜欢
    • 2012-07-28
    • 2015-10-23
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多