{
"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(“”);
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());
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/edb181516128449cab22708e2b1aed57.png)
![img](https://img-blog.csdnimg.cn/img_convert/9bb72a49c7c604606b9b2bbb04c09249.png)
![img](https://img-blog.csdnimg.cn/img_convert/3667e5895b6c673463088f644c26fa4c.png)
![img](https://img-blog.csdnimg.cn/img_convert/ecbbb1dd59014034aef9692c5d2141e2.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**