Q:非洲食人族的酋长吃什么?

JAVA API java模糊查询需要配置中文分词么

Elasticsearch | 作者 心有千千结 | 发布于2018年10月30日 | 阅读数:5374

在es上直接查询是可以用中文词组的

1.png

 
但是用java api 查询 "监控"没数据,但是用将他们拆分就可以查询。
 @Test
public void highlight(){
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<span class='highlight'>");
highlightBuilder.postTags("</span>");
highlightBuilder.field("@line_message");
//"@line_message": "2018-10-29 15:51:58.572 - [Thread-4] INFO com.pangu.saas.jmx.JmxApp - flume日志1540799518572"

SearchResponse response = client.prepareSearch("f1db322c07f7426780158bdda8d314d1")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
// .setSearchType(SearchType.DEFAULT)
.setQuery(
QueryBuilders.boolQuery()
.must(QueryBuilders.wildcardQuery("@line_message","*监控*"))
// .must(QueryBuilders.wildcardQuery("@line_message","*监*"))
)

// .setQuery(QueryBuilders.matchQuery("resid","100"))

.setFrom(0).setSize(60).setExplain(true)
.highlighter(highlightBuilder)
.get();

SearchHits hits = response.getHits();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSourceAsMap();
list.add(source);
System.out.println(hit.getSourceAsString());
hit.getHighlightFields().forEach((k,v)->{
System.out.println(k);
System.out.println(v);
Text[] fragments = v.getFragments();
String hightStr = "";
for(Text text : fragments){
hightStr += text.toString();
}
System.out.println("hightStr = " + hightStr);
source.put(k,hightStr);
});
System.out.println(list);
// System.out.println(hit.getSourceAsString());
// System.out.println(hit.getHighlightFields());
}
}
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

把查询的dsl打印出来,放在kibana或者head插件,很容易就发现问题的原因了。

rochy - rochy_he

赞同来自:

请尝试使用下面的代码
.setQuery(
QueryBuilders.boolQuery()
.must(QueryBuilders.wildcardQuery("@line_message.keyword","*监控*"))
)

心有千千结

赞同来自:

我加了高亮的代码
这种方式执行结果是可以高亮的:

.must(QueryBuilders.matchPhrasePrefixQuery("@line_message","监控"))
结果:
[{filename=flume, @timestamp=2018-10-30T10:17:53.053Z, @line_message=2018-10-30 18:18:39.532 [Thread-4] INFO  com.pangu.saas.jmx.JmxApp - flume<span   class='highlight'>监</span><span   class='highlight'>控</span>日志1540894719532, level=INFO, @version=1, className=com.pangu.saas.jmx.JmxApp, time=2018-10-30 18:18:39.532, thread=Thread-4, type=f1db322c07f7426780158bdda8d314d1, resid=f1db322c07f7426780158bdda8d314d1, @hostname=10.200.132.160}]
 
这种方式可以查出结果,但是不高亮显示
.must(QueryBuilders.wildcardQuery("@line_message.keyword","*监控*"))
结果:
 
[{filename=flume, @timestamp=2018-10-30T10:17:53.053Z, @line_message=2018-10-30 18:18:39.532 [Thread-4] INFO  com.pangu.saas.jmx.JmxApp - flume监控日志1540894719532, level=INFO, @version=1, className=com.pangu.saas.jmx.JmxApp, time=2018-10-30 18:18:39.532, thread=Thread-4, type=f1db322c07f7426780158bdda8d314d1, resid=f1db322c07f7426780158bdda8d314d1, @hostname=10.200.132.160}]
 
 
 
public static void highlight(){
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<span class='highlight'>");
highlightBuilder.postTags("</span>");
highlightBuilder.field("@line_message");
//"@line_message": "2018-10-29 15:51:58.572 - [Thread-4] INFO com.pangu.saas.jmx.JmxApp - flume日志1540799518572"

SearchResponse response = client.prepareSearch("f1db322c07f7426780158bdda8d314d1")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
// .setSearchType(SearchType.DEFAULT)
.setQuery(
QueryBuilders.boolQuery()
// .must(QueryBuilders.wildcardQuery("@line_message","*监控*"))
.must(QueryBuilders.matchPhrasePrefixQuery("@line_message","监控"))
// .must(QueryBuilders.wildcardQuery("@line_message.keyword","*监控*"))
// .must(QueryBuilders.matchPhrasePrefixQuery("@line_message","flume"))
// .must(QueryBuilders.wildcardQuery("@line_message","*监*"))
)

// .setQuery(QueryBuilders.matchQuery("resid","100"))

.setFrom(0).setSize(1).setExplain(true)
.highlighter(highlightBuilder)
.get();

SearchHits hits = response.getHits();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSourceAsMap();
list.add(source);
System.out.println(hit.getSourceAsString());
hit.getHighlightFields().forEach((k,v)->{
System.out.println(k);
System.out.println(v);
Text[] fragments = v.getFragments();
String hightStr = "";
for(Text text : fragments){
hightStr += text.toString();
}
System.out.println("hightStr = " + hightStr);
source.put(k,hightStr);
});
System.out.println(list);
// System.out.println(hit.getSourceAsString());
// System.out.println(hit.getHighlightFields());
}
}

要回复问题请先登录注册