Intuizione: In SQL gergo:
Select distinct full_name from authors;
è equivalente a
Select full_name from authors group by full_name;
Quindi, possiamo usare il raggruppamento/sintassi aggregato in elasticsearch per trovare le voci distinte .
Assumere il seguente è la struttura memorizzata in elasticsearch:
[{
"author": "Brian Kernighan"
},
{
"author": "Charles Dickens"
}]
cosa non ha funzionato: Plain aggregazione
{
"aggs": {
"full_name": {
"terms": {
"field": "author"
}
}
}
}
ho ottenuto il seguente errore:
{
"error": {
"root_cause": [
{
"reason": "Fielddata is disabled on text fields by default...",
"type": "illegal_argument_exception"
}
]
}
}
Che cosa ha funzionato come un fascino: aggiungendo .keyword con il campo
{
"aggs": {
"full_name": {
"terms": {
"field": "author.keyword"
}
}
}
}
E l'uscita di esempio potrebbe essere:
{
"aggregations": {
"full_name": {
"buckets": [
{
"doc_count": 372,
"key": "Charles Dickens"
},
{
"doc_count": 283,
"key": "Brian Kernighan"
}
],
"doc_count": 1000
}
}
}
Bonus suggerimento:
Assumiamo il campo in que Bustione è annidato come segue:
[{
"authors": [{
"details": [{
"name": "Brian Kernighan"
}]
}]
},
{
"authors": [{
"details": [{
"name": "Charles Dickens"
}]
}]
}
]
Ora la query giusta diventa:
{
"aggregations": {
"full_name": {
"aggregations": {
"author_details": {
"terms": {
"field": "authors.details.name"
}
}
},
"nested": {
"path": "authors.details"
}
}
},
"size": 0
}
cosa vuol FULL_NAME significa? – neustart47
@ neustart47 full_name è solo il nome delle aggregazioni –