2014-05-20 3 views
7

다음 쿼리를 사용하여 아래의 데이터에 대한 탄성 검색을 사용하여 용어 집계를 수행하려고하면 출력 결과가 토큰으로 분리됩니다 (아래 출력 참조). 그래서 os_name을 multi_field로 매핑하려고 시도했지만 지금은 쿼리 할 수 ​​없습니다. 토큰없이 색인을 가질 수 있습니까? "페도라 코어"와 같은ElasticSearch 용어 집계

검색어 :

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

데이터 :

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

출력 :

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

MAPP 보내고 : 일하는 것이

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

너무 당신의 매핑을 게시하시기 바랍니다 . – Thorsten

+0

안녕하세요 @ 토르스틴, 매핑을 추가했습니다. 감사. – codeBarer

답변

6

는 사실이

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

처럼 매핑을 변경해야하고 aggs로 변경해야합니다

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

하나의 해결책은 not_analyzed (the docs for attribute "index"에 대해 자세히 알아)에 필드를 설정하는 것입니다.

이 솔루션은 요구 사항에 따라 입력을 전혀 분석하지 않으므로 custom analyzer과 같이 설정할 수 있습니다. 단어를 분할하지 않고 소문자로 구분하여 대소 문자를 구분하지 않습니다.

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

출력 :

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

매우 근사합니다. 무리 감사. 나는 정말로 os_name을 분석 할 필요가 없다 ... 나는 생각한다. :) – codeBarer