2017-01-12 2 views
0

Je voudrais faire une requête avec UNION et limite.Comment faire une requête union sur Elasticsearch?

Je peux expliquer cette requête sur mysql.

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

Je l'ai essayé sur 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}}}]}} Ensuite, l'erreur est survenue.

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

Lorsque j'utilise uniquement une requête de correspondance, cela a fonctionné. Mais avec la taille, ça ne marche pas.

Comment puis-je le résoudre?

Répondre

2

Le chemin à parcourir est MultiSearch

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

Merci, Il fonctionne sur boucle. Mais Comment l'utiliser sur URI Search? – USER

+0

@USER Pourquoi voulez-vous faire cela lors de la recherche d'URI? –

+0

Ou, comment puis-je l'envoyer à un paramètre ajax? – USER