Ken*_*edy 21 java lucene search token elasticsearch
如何在结果中返回特定字段的标记
例如,A GET请求
curl -XGET 'http://localhost:9200/twitter/tweet/1'
Run Code Online (Sandbox Code Playgroud)
回报
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_source" : {
"user" : "kimchy",
"postDate" : "2009-11-15T14:12:12",
"message" : "trying out Elastic Search"
}
}
Run Code Online (Sandbox Code Playgroud)
我想在结果中包含'_source.message'字段的标记
imo*_*tov 29
还有另一种方法可以使用以下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"
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
重要的是要注意,虽然此脚本返回索引的实际术语,但它还会缓存所有字段值,而大型索引可以使用大量内存.因此,对于大型索引,从存储的字段或源中检索字段值并使用以下MVEL脚本在运行中再次重新分析它们可能更有用:
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
Run Code Online (Sandbox Code Playgroud)
此MVEL脚本可以存储为config/scripts/analyze.mvel以下查询并与之一起使用:
curl 'http://localhost:9200/test-idx/_search?pretty=true' -d '{
"query" : {
"match_all" : { }
},
"script_fields": {
"terms" : {
"script": "analyze",
"params": {
"field": "message"
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
如果您指的是已编制索引的令牌,则可以在消息字段中创建术语构面.增加size值以获得更多条目,或设置0为获取所有条目.
Lucene提供了存储术语向量的能力,但到目前为止还没有办法使用elasticsearch访问它(据我所知).
你为什么需要那个?如果您只想检查索引的内容,可以查看analyze api.
| 归档时间: |
|
| 查看次数: |
15738 次 |
| 最近记录: |