本篇文章为你整理了ElasticSearch 实现分词全文检索()的详细内容,包含有 ElasticSearch 实现分词全文检索,希望能帮助你了解 ElasticSearch 实现分词全文检索。
fuzzy查询:模糊查询,我们输入字符的大概,ES就可以
wildcard 查询:通配查询,和MySQL中的 like 差不多,可以在查询时,在字符串中指定通配符 * 和占位符?
range 查询:范围查询,只针对数值类型,对某一个Field进行大于或小于的范围指定查询
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 实现分词全文检索 - 测试数据准备
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);
以上就是ElasticSearch 实现分词全文检索()的详细内容,想要了解更多 ElasticSearch 实现分词全文检索的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。