2016-06-03 12 views
0

Ich benutze die php library for elasticsearch Und ich bin triying, um eine Zählung von einer Suche zu erhalten, mit der Zählmethode, um zu vermeiden, alle Ergebnisse zu holen. Aber anscheinend verwende ich nicht das richtige Format oder etwas, wenn ich den elasticsearch Server abfrage.Korrekte Abfrage für Zählung auf elasticsearch php Bibliothek

Hier ist, was ich

 $hosts = ['localhost:9200']; 
     $client = ClientBuilder::create() 
     ->setHosts($hosts) 
     ->build(); 


     $params = [ 
     'index' => 'logstash-*', 
     'type' => 'INFO', 
     'body' => [ 
      'query' => [ 
       'bool' => [ 
        "should" => [ 
         ["term" => ["tags" => "producer"]], 
         ["term" => ["tags" => "statistics"]], 
         ["term" => ["message" => "view"]], 
         ["term" => ["context.id" => 1]] 
        ] 
       ] 
      ] 
     ] 
    ]; 


$response = $client->search($params); // was returning all the results too, I was wrong 
$response = $client->count($params); // return a count of all my documents in elasticsearch instance, that's wrong 

FIXED thanx zu @val tun habe ich die minimun bis 4 entsprechen sollte und es funktioniert!

array (size=4) 
    'took' => int 14 
    'timed_out' => boolean false 
    '_shards' => 
    array (size=3) 
     'total' => int 10 
     'successful' => int 10 
     'failed' => int 0 
    'hits' => 
    array (size=3) 
     'total' => int 11 
     'max_score' => float 7.852423 
     'hits' => 
     array (size=10) 
      0 => 
      array (size=5) 
       ... 
      1 => 
      array (size=5) 
       ... 
      2 => 
      array (size=5) 
       ... 
      3 => 
      array (size=5) 
       ... 
      4 => 
      array (size=5) 
       ... 
      5 => 
      array (size=5) 
       ... 
      6 => 
      array (size=5) 
       ... 
      7 => 
      array (size=5) 
       ... 
      8 => 
      array (size=5) 
       ... 
      9 => 
      array (size=5) 
       ... 
array (size=2) 
    'count' => int 11 
    '_shards' => 
    array (size=3) 
     'total' => int 10 
     'successful' => int 10 
     'failed' => int 0 

Wie kann ich die richtige Anzahl für die Abfrage erhalten?

+1

Was passiert, wenn Sie hinzufügen, ' "minimum_should_match"=> 1 'in der' bool' Abfrage? – Val

+0

@val es die gleiche Anzahl zurückgibt, eine schlechte mit allen Dokumenten auf der Elasticsearch Instanz { Array (size = 2) 'count' => int 6539 '_shards' => Array (size = 3) 'total' => int 10 'erfolgreich' => int 10 'fehlgeschlagen' => int 0 } – Surt

+1

Können Sie Ihre Frage mit dem Ergebnis, das Sie von jedem Anruf erhalten, bitte aktualisieren? – Val

Antwort

0

Ich bearbeitet die Frage mit der richtigen Antwort. Die letzte Frage ist:

$params = [ 
    'index' => 'logstash-*', 
    'type' => 'INFO', 
    'body' => [ 
     'query' => [ 
      'bool' => [ 
       "should" => [ 
        ["term" => ["tags" => "producer"]], 
        ["term" => ["tags" => "statistics"]], 
        ["term" => ["message" => "view"]], 
        ["term" => ["context.id" => 1]] 
       ], 
       "minimum_should_match" => 4 
      ] 
     ] 
    ] 
]; 
+1

Aber wenn alle Termabfragen übereinstimmen sollten, ist es wahrscheinlich besser, wenn Sie 'bool/must' benutzen oder wenn Sie auf ES2' bool/filter' sind – Val