ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询
目录
ElasticSearch 实现分词全文检索 - 概述
ElasticSearch 实现分词全文检索 - ES、Kibana、IK安装
ElasticSearch 实现分词全文检索 - Restful基本操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - 测试数据准备
ElasticSearch 实现分词全文检索 - term、terms查询
ElasticSearch 实现分词全文检索 - match、match_all、multimatch查询
ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询
ElasticSearch 实现分词全文检索 - Scroll 深分页
ElasticSearch 实现分词全文检索 - delete-by-query
ElasticSearch 实现分词全文检索 - 复合查询
ElasticSearch 实现分词全文检索 - filter查询
ElasticSearch 实现分词全文检索 - 高亮查询
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality
ElasticSearch 实现分词全文检索 - 经纬度查询
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(suggest)
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码
数据准备
ElasticSearch 实现分词全文检索 - 测试数据准备
id 查询
# id 查询
GET /sms-logs-index/_doc/1
Java
@Test
void idQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
GetRequest request = new GetRequest(indexName,"1");
//2. 执行查询
GetResponse resp = client.get(request, RequestOptions.DEFAULT);
//4. 获取到 中的数据,并展示
System.out.println(resp.getSourceAsMap());
}
ids 查询
根据多个ID查询,类似MySQL中的 where id in (1,2,3)
# ids 查询
GET /sms-logs-index/_search/
{
"query": {
"ids": {
"values": ["1","2","3"]
}
}
}
Java
@Test
void idsQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.idsQuery().addIds("1","2","3"));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
prefix查询
前缀查询,可以通过一个关键字去指定一个Field的前缀,从而查询到指定的文档
# prefix 查询
GET /sms-logs-index/_search/
{
"query": {
"prefix": {
"longCode": {
"value": "李"
}
}
}
}
Java
@Test
void prefixQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.prefixQuery("longCode","李"));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
fuzzy查询
模糊查询,我们输入字符的大概,ES就可以
# fuzzy 查询
GET /sms-logs-index/_search/
{
"query": {
"fuzzy": {
"smsContent": {
"value": "天天凯心", # 可以有错别字
"prefix_length": 2 # 前N个字符,不允许出现错误,完本匹配
}
}
}
}
Java
@Test
void fuzzyQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.fuzzyQuery("smsContent","天天凯心").prefixLength(2));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
wildcard 查询
通配查询,和MySQL中的 like 差不多,可以在查询时,在字符串中指定通配符 * 和占位符?
# wildcard 查询
GET /sms-logs-index/_search/
{
"query": {
"wildcard": {
"smsContent": {
"value": "天*"
}
}
}
}
Java
@Test
void wildcardQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.wildcardQuery("smsContent","天*"));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
range 查询
范围查询,只针对数值类型,对某一个Field进行大于或小于的范围指定查询
# wildcard 查询
GET /sms-logs-index/_search/
{
"query": {
"range": {
"fee": {
"gte": 10,
"lte": 20
}
}
}
}
Java
@Test
void rangeQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.rangeQuery("fee").gte(10).lte(20));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
regexp 查询
正则查询,通过你编写的正则表达式去匹配内容
PS:prefix,fuzzy,wildcard 和 regexp 查询效率相对比较低。要求效率比较高时,避免去使用]
# regexp 查询
POST /sms-logs-index/_search/
{
"query": {
"regexp": {
"mobile": "1382[0-9]{7}" # 查询 1382开头的手机号
}
}
}
}
Java
@Test
void regexpQuery() throws Exception{
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建GetRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.regexpQuery("mobile","1382[0-9]{7}"));
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 获取到 _source 中的数据,并展示
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result);
}
}
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/17168115.html