2014-05-20 10 views
7

Ich versuche, eine Term-Aggregation mit elastischer Suche für die folgenden Daten mit folgenden Abfrage durchzuführen, bricht die Ausgabe die Namen in Tokens (siehe Ausgabe unten). Also habe ich versucht, den os_name als multi_field abzubilden und kann nun nicht mehr abfragen. Ist es möglich, Index ohne Token zu haben? wie "Fedora Core"?ElasticSearch Begriff Aggregation

Abfrage:

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

Daten:

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

Ausgang:

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

bitte posten Sie Ihre Mapping . – Thorsten

+0

Hallo @Thorsten, ich habe auch das Mapping hinzugefügt. Vielen Dank. – codeBarer

Antwort

6

Eigentlich sollten Sie Ihre Mapping wie diese

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

ändern und Ihre aggs sollte geändert werden: zu

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

Eine Lösung, die funktionieren würde, ist das Feld not_analyzed (Lesen Sie mehr darüber in the docs for attribute "index") einzustellen.

Diese Lösung analysiert die Eingabe überhaupt nicht. Abhängig von Ihren Anforderungen können Sie eine custom analyzer, z. um die Wörter nicht zu teilen, aber Kleinbuchstaben, um die Groß- und Kleinschreibung zu vermeiden.

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

Ausgang:

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

Sehr cool! Vielen Dank. Ich muss den os_name nicht wirklich analysieren ... ich denke :) – codeBarer

Verwandte Themen