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 实现分词全文检索 - 测试数据准备
复合过滤器,将多个查询条件,以一定的逻辑组合在一起
# 查询省份为江苏或上海
# operatorld 不是联通 !=2
# smsContent 中包括 开心 和 钞票
# bool 查询
POST /sms-logs-index/_search
{
"query": {
"bool": {
"should": [
{"term": {
"province": {
"value": "江苏"
}
}},
{"term": {
"province": {
"value": "上海"
}
}}
],
"must_not": [
{
"term": {
"operatorld": {
"value": "2"
}
}
}
],
"must": [
{
"match": {
"smsContent": "开心"
}
},
{
"match": {
"smsContent": "钞票"
}
}
]
}
}
}
JAVA
@Test
void boolQuery() throws Exception {
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建SearchRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.should(QueryBuilders.termQuery("province","江苏"));
boolQuery.should(QueryBuilders.termQuery("province","上海"));
boolQuery.mustNot(QueryBuilders.termQuery("operatorld","2"));
boolQuery.must(QueryBuilders.matchQuery("smsContent","开心"));
boolQuery.must(QueryBuilders.matchQuery("smsContent","钞票"));
builder.query(boolQuery);
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 输出返回值
for (SearchHit hit : resp.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
boosting 查询可以帮助我们去影响查询后的 score
POST /sms-logs-index/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"smsContent": "人"
}
},
"negative": {
"match": {
"smsContent": "网络" # 如果查出来的包括 网络
}
},
"negative_boost": 0.5 #将分数乘以系数 0.5 ,分数越高,排名越靠前
}
}
}
Java
@Test
void boostringQuery() throws Exception {
String indexName = "sms-logs-index";
RestHighLevelClient client = ESClient.getClient();
//1. 创建SearchRequest对象
SearchRequest request = new SearchRequest(indexName);
//2. 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
BoostingQueryBuilder boostingQuery = QueryBuilders.boostingQuery(
QueryBuilders.matchQuery("smsContent", "人"),
QueryBuilders.matchQuery("smsContent", "网络")
).negativeBoost(0.5f);
builder.query(boostingQuery);
request.source(builder);
//3. 执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4. 输出返回值
for (SearchHit hit : resp.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
ES 是一个使用Java语言并且基于Lucene编写的搜索引擎框架,他提供了分布式的全文搜索功能,提供了一个统一的基于Restful风格的WEB接口,官方客户端也对多种语言都提供了相应的API。