2012-11-01 6 views
19

가 어떻게 특정 필드의 토큰이 결과 예를 들어elasticsearch -

, GET 요청에 반환 할 수 있습니다

curl -XGET 'http://localhost:9200/twitter/tweet/1' 

반환

{ 
    "_index" : "twitter", 
    "_type" : "tweet", 
    "_id" : "1", 
    "_source" : { 
     "user" : "kimchy", 
     "postDate" : "2009-11-15T14:12:12", 
     "message" : "trying out Elastic Search" 
    } 
} 

필드의 토큰을 돌려줍니다 '_source.message'필드의 토큰을 결과에 포함하고 싶습니다.

답변

27

다음 script_fields 스크립트를 사용하여 할 수있는 다른 방법도 있습니다 :

curl 'http://localhost:9200/test-idx/_search?pretty=true' -d '{ 
    "query" : { 
     "match_all" : { } 
    }, 
    "script_fields": { 
     "terms" : { 
      "script": "doc[field].values", 
      "params": { 
       "field": "message" 
      } 
     } 

    } 
}' 

는이 스크립트가 색인 된 실제 조건을 반환하면서, 또한 모든 필드 값을 캐시 및 대형에 유의하는 것이 중요합니다 인덱스는 많은 메모리를 사용할 수 있습니다.

import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; 
import java.io.StringReader; 

// Cache analyzer for further use 
cachedAnalyzer=(isdef cachedAnalyzer)?cachedAnalyzer:doc.mapperService().documentMapper(doc._type.value).mappers().indexAnalyzer(); 

terms=[]; 
// Get value from Fields Lookup 
//val=_fields[field].values; 

// Get value from Source Lookup 
val=_source[field]; 

if(val != null) { 
    tokenStream=cachedAnalyzer.tokenStream(field, new StringReader(val)); 
    CharTermAttribute termAttribute = tokenStream.addAttribute(CharTermAttribute); 
    while(tokenStream.incrementToken()) { 
    terms.add(termAttribute.toString()) 
    }; 
    tokenStream.close(); 
} 
terms 

이 MVEL 스크립트 config/scripts/analyze.mvel로 저장하고 사용할 수 있습니다 : 그래서, 대형 인덱스에, 저장 필드 또는 소스에서 필드 값을 검색하고 다음 MVEL 스크립트를 사용하여 즉시 다시 재분석하는 것이 더 유용 할 수 있습니다 다음 검색어와 함께 :

curl 'http://localhost:9200/test-idx/_search?pretty=true' -d '{ 
    "query" : { 
     "match_all" : { } 
    }, 
    "script_fields": { 
     "terms" : { 
      "script": "analyze", 
      "params": { 
       "field": "message" 
      } 
     } 

    } 
}' 
+3

이것은 무서운 !!! 좋은 대답! – javanna

+0

그것은 무섭지만 재미 있습니다. :) DocLookup에서 IndexReader에 액세스 할 수 있었으면 좋겠다. (거기에 있지만, 지금은 비공개이다.) 그런 다음 두 번째 텍스트를 다시 분석하는 대신 용어 벡터를 사용할 수있었습니다. – imotov

+0

네, 물론입니다. 스크립트없이 단어 벡터를 읽는 것도 좋을까요, 아니면 플러그인을 통해서 읽는 것이 좋을까요? – javanna

6

토큰을 의미하는 경우 ens가 색인 된 경우 메시지 필드에 terms facet을 만들 수 있습니다. 더 많은 항목을 다시 얻으려면 size 값을 늘리거나 모든 조건을 얻으려면 0으로 설정하십시오.

Lucene은 용어 벡터를 저장할 수있는 기능을 제공하지만 지금까지는 탄성 검색으로 액세스 할 수있는 방법이 없습니다 (아는 한).

왜 필요한가요? 색인을 생성하는 항목 만 확인하려는 경우 analyze api을 살펴보십시오.

+0

니스, 이것이 내가 찾고있는 것입니다. 감사합니다 – Kennedy