elasticsearch 基于ik分词器的分词查询和模糊匹配
·
前言:elasticsearch 查询有很多关键字,查询的条件有固定格式,返回结果提示不明确,让ES使用起来有点不方便的感觉,ES查询方式很多,简单介绍几种使用点的,实用的
此处简单梳理一下最常用的查询
模糊匹配查询
类似 mysql 语法中的 like ‘%value%’
http://localhost:9200/fileindex1/_search
{
"query": {
"match_phrase_prefix": {
"name": {
"query": "测试",
"max_expansions": 50
}
}}
}
@Test
void testSearch2() throws IOException {
SearchRequest searchRequest = new SearchRequest("fileindex1");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件,我们可以使用SearchSourceBuilder工具来实现
//精确匹配QueryBuilders.termQuery()
MatchPhrasePrefixQueryBuilder matchQueryBuilder = QueryBuilders.matchPhrasePrefixQuery("name","测试");
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse));
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
类似于百度的分词查询
将查询的内容分解,提取。例如 文档数据库 ,就会被分词器 分解为 文档 ,数据 ,数据库等 查询返回更加有广度
分词查询接口
http://localhost:9200/_analyze
{
"analyzer":"ik_max_word",
"text":"文档数据库"
}
分词解析
{
"111": [
{
"111": "文档",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"111": "数据库",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 1
},
{
"111": "数据",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 2
},
{
"111": "库",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 3
}
]
}
查询会将分词解析出的分词
ES查询条件 分词查询
http://localhost:9200/fileindex1/_search
{
"query": {
"match": {
"name": "文档数据库"
}
}
}
@Test
void testSearch1() throws IOException {
SearchRequest searchRequest = new SearchRequest("fileindex1");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件,我们可以使用SearchSourceBuilder工具来实现
//精确匹配QueryBuilders.termQuery()
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name","测试");
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse));
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
根据具体字段精确查询内容
{
"query": {
"fuzzy": {
"attachment.author.keyword": "测试"
}
}
}
// 根据字段精准查询
@Test
void testSearch3() throws IOException {
SearchRequest searchRequest = new SearchRequest("fileindex1");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件,我们可以使用SearchSourceBuilder工具来实现
//精确匹配QueryBuilders.termQuery()
FuzzyQueryBuilder matchQueryBuilder = QueryBuilders.fuzzyQuery("attachment.author.keyword","user
");
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse));
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
ES 高亮显示查询
http://localhost:9200/fileindex1/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"attachment.author": "NOOB"
}
},
{
"match": {
"attachment.content": "省政府"
}
}
]
}
},
"highlight": {
"fields": {
"attachment.author": {},
"attachment.content": {}
}
}
}
java 实现方式
void testSearch2() throws IOException {
SearchRequest searchRequest = new SearchRequest("fileindex1");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件,我们可以使用SearchSourceBuilder工具来实现
//精确匹配QueryBuilders.termQuery()
MatchPhrasePrefixQueryBuilder matchQueryBuilder = QueryBuilders.matchPhrasePrefixQuery("attachment.content","皇帝");
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
// 高亮显示
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("attachment.author").field("attachment.content");
highlightBuilder.preTags("<span style=\"color: red\">");
highlightBuilder.postTags("</span>");
sourceBuilder.highlighter(highlightBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//System.out.println(JSON.toJSONString(searchResponse));
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
System.out.println(JSON.toJSONString(searchResponse.getHits()));
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getHighlightFields());
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)