2014-05-20 21 views
7

Próbuję przeprowadzić agregację terminów za pomocą elastycznego wyszukiwania dla poniższych danych z następującym zapytaniem, dane wyjściowe dzielą nazwy na tokeny (patrz dane wyjściowe poniżej). Tak więc próbowałem mapować nazwę os_na jako multi_field i teraz nie jestem w stanie zapytać przez nią. Czy możliwe jest posiadanie indeksu bez żetonów? takie jak "Fedora Core"?ElasticSearch agregacja terminów

Zapytanie:

GET /temp/example/_search 
{ 
    "size": 0, 
    "aggs": { 
    "OS": { 
     "terms": { 
      "field": "os_name" 
     } 
    } 
    } 
} 

danych:

... 
    { 
     "_index": "temp", 
     "_type": "example", 
     "_id": "3", 
     "_score": 1, 
     "_source": { 
      "title": "system3", 
      "os_name": "Fedora Core", 
      "os_version": 18 
     } 
    }, 
    { 
     "_index": "temp", 
     "_type": "example", 
     "_id": "1", 
     "_score": 1, 
     "_source": { 
      "title": "system1", 
      "os_name": "Fedora Core", 
      "os_version": 20 
     } 
    }, 
    { 
     "_index": "temp", 
     "_type": "example", 
     "_id": "2", 
     "_score": 1, 
     "_source": { 
      "title": "backup", 
      "os_name": "Yellow Dog", 
      "os_version": 6 
     } 
    } 
... 

wyjściowa:

 ... 
     { 
      "key": "core", 
      "doc_count": 2 
     }, 
     { 
      "key": "fedora", 
      "doc_count": 2 
     }, 
     { 
      "key": "dog", 
      "doc_count": 1 
     }, 
     { 
      "key": "yellow", 
      "doc_count": 1 
     } 
     ... 

MAPP ing:

PUT /temp 
{ 
    "mappings": { 
    "example": { 
     "properties": { 
     "os_name": { 
      "type": "string" 
     }, 
     "os_version": { 
      "type": "long" 
     }, 
     "title": { 
      "type": "string" 
     } 
     } 
    } 
    } 
} 
+0

proszę zamieścić swoje odwzorowanie zbyt . – Thorsten

+0

Hi @ Thorsten, dodałem również mapowanie. Dzięki. – codeBarer

Odpowiedz

6

Właściwie trzeba zmienić mapowanie jak ten

"os_name": { 
    "type": "string", 
    "fields": { 
    "raw": { 
     "type": "string", 
     "index": "not_analyzed" 
    } 
    } 
}, 

i twoi aggs powinno być zmienione na:

GET /temp/example/_search 
{ 
    "size": 0, 
    "aggs": { 
    "OS": { 
     "terms": { 
      "field": "os_name.raw" 
     } 
    } 
    } 
} 
4

Jedno rozwiązanie, które będzie działać jest stworzenie pola do not_analyzed (więcej o tym w the docs for attribute "index").

To rozwiązanie w ogóle nie analizuje danych wejściowych, w zależności od wymagań, które można ustawić, np. custom analyzer, np. nie rozdzielać słów, ale małe litery, aby uzyskać wyniki niewrażliwe na wielkość liter.

curl -XDELETE localhost:9200/temp 
curl -XPUT localhost:9200/temp -d ' 
{ 
    "mappings": { 
    "example": { 
     "properties": { 
     "os_name": { 
      "type": "string", 
      "index" : "not_analyzed" 
     }, 
     "os_version": { 
      "type": "long" 
     }, 
     "title": { 
      "type": "string" 
     } 
     } 
    } 
    } 
}' 

curl -XPUT localhost:9200/temp/example/1 -d ' 
{ 
    "title": "system3", 
    "os_name": "Fedora Core", 
    "os_version": 18 
}' 

curl -XPUT localhost:9200/temp/example/2 -d ' 
{ 
    "title": "system1", 
    "os_name": "Fedora Core", 
    "os_version": 20 
}' 

curl -XPUT localhost:9200/temp/example/3 -d ' 
{ 
    "title": "backup", 
    "os_name": "Yellow Dog", 
    "os_version": 6 
}' 

curl -XGET localhost:9200/temp/example/_search?pretty=true -d ' 
{ 
    "size": 0, 
    "aggs": { 
    "OS": { 
     "terms": { 
      "field": "os_name" 
     } 
    } 
    } 
}' 

wyjściowa:

{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 3, 
    "max_score" : 0.0, 
    "hits" : [ ] 
    }, 
    "aggregations" : { 
    "OS" : { 
     "buckets" : [ { 
     "key" : "Fedora Core", 
     "doc_count" : 2 
     }, { 
     "key" : "Yellow Dog", 
     "doc_count" : 1 
     } ] 
    } 
    } 
} 
+0

Bardzo fajnie! Wielkie dzięki. Naprawdę nie muszę analizować nazwy os ... Myślę, że :) – codeBarer