2014-07-09 25 views
7

Sto imparando la ricerca elastica e vorrei contare valori distinti. Finora posso contare valori ma non distinti.Contare valori distinti utilizzando elasticsearch

Ecco i dati di esempio:

curl http://localhost:9200/store/item/ -XPOST -d '{ 
    "RestaurantId": 2, 
    "RestaurantName": "Restaurant Brian", 
    "DateTime": "2013-08-16T15:13:47.4833748+01:00" 
}' 

curl http://localhost:9200/store/item/ -XPOST -d '{ 
    "RestaurantId": 1, 
    "RestaurantName": "Restaurant Cecil", 
    "DateTime": "2013-08-16T15:13:47.4833748+01:00" 
}' 

curl http://localhost:9200/store/item/ -XPOST -d '{ 
    "RestaurantId": 1, 
    "RestaurantName": "Restaurant Cecil", 
    "DateTime": "2013-08-16T15:13:47.4833748+01:00" 
}' 

E quello che ho provato finora:

curl -XPOST "http://localhost:9200/store/item/_search" -d '{ 
    "size": 0, 
    "aggs": { 
    "item": { 
     "terms": { 
     "field": "RestaurantName" 
     } 
    } 
    } 
}' 

uscita:

{ 
    "took": 0, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 3, 
    "max_score": 0.0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "item": { 
     "buckets": [ 
     { 
      "key": "restaurant", 
      "doc_count": 3 
     }, 
     { 
      "key": "cecil", 
      "doc_count": 2 
     }, 
     { 
      "key": "brian", 
      "doc_count": 1 
     } 
     ] 
    } 
    } 
} 

Come posso ottenere il conto di cecil come 1 invece di 2

risposta

4
+0

Ho anche provato: curl -XPOST "http: // localhost: 9200/store/item/_search" -d '{"size": 0, "aggs": {"item": {"cardinality": { "campo": "RestaurantName"}}}} 'ma non ottiene conteggi distinti – Developer

+0

Dovrebbe essere curl -XGET? – c24b

+0

e va bene rinominare l'aggregato come elemento? – c24b

3

Devi usare l'opzione cardinalità come menzionati da @coder che si può trovare nella doc

curl -XGET "http://localhost:9200/store/item/_search" -d' 
{ 
"aggs" : { 
    "restaurant_count" : { 
     "cardinality" : { 
      "field" : "RestaurantName", 
      "precision_threshold": 100, 
      "rehash": false 
      } 
      } 
     } 
}' 

Questo ha funzionato per me ...

0

Non c'è alcun supporto per il conteggio distinto in ElasticSearch, sebbene esista un conteggio non deterministico. Utilizzare l'aggregazione "termini" e contare i bucket nel risultato. Vedi domanda Count distinct on elastic search .

Problemi correlati