mapping属性
mapping是对索引库中文档的约束, 常见的mapping属性包括:
-
type
: 字段数据类型,常见的简单类型有:-
字符串
: text(可分词的文本), keyword(精确值, 例如: 品牌,国家) -
数值
: long, integer, short, byte, double, float -
布尔
: boolean -
日期
: date -
对象
: object
-
-
index
: 是否创建索引, 默认为true -
analyzer
: 使用哪种分词器 -
properties
: 该字段的子字段
索引库操作
创建索引库
PUT /zyw
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
查看索引库
GET /zyw
GET /zyw/_mapping
# 查看索引库列表和健康状态
GET /_cat/indices
删除索引库
DELETE /zyw
修改索引库, 添加新字段
索引库和mapping一旦创建无法修改, 但是可以添加新字段
PUT /zyw/_mapping
{
"properties": {
"age": {
"type": "integer"
}
}
}
文档操作
新增文档
POST /zyw/_doc/1
{
"info": "java是最好的语言",
"email": "zy@163.com",
"name": {
"firstName": "云",
"lastName": "赵"
}
}
可以不指定id, es会自动生成id
查询文档
GET /zyw/_doc/1
删除文档
DELETE /zyw/_doc/1
修改文档
- 全量修改, 会删除旧文档, 添加新文档
PUT /zyw/_doc/1
{
"info": "java是最好的语言",
"email": "zy@163.com",
"name": {
"firstName": "云",
"lastName": "赵"
}
}
- 局部修改
POST /zyw/_update/1
{
"doc": {
"email": "test@163.com"
}
}
RestClient操作索引库
- 引入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
注意:
springboot管理了elasticsearch的部分依赖, 查看springboot的依赖管理
我们需要在pom文件中定义这个版本值,覆盖springboot的
- HotelDoc.java
@Data
@NoArgsConstructor
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String location;
private String pic;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
}
}
- hotel索引库
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address": {
"type": "keyword",
"index": false
},
"price": {
"type": "integer"
},
"score": {
"type": "integer"
},
"brand": {
"type": "keyword",
"copy_to": "all"
},
"city": {
"type": "keyword"
},
"starName": {
"type": "keyword"
},
"business": {
"type": "keyword",
"copy_to": "all"
},
"location": {
"type": "geo_point"
},
"pic": {
"type": "keyword",
"index": false
},
"all": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
基于elasticsearch的规则, id用keyword
- 操作索引库
import com.zyw.elasticsearchdemo.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class ElasticsearchDemoApplicationTests {
private RestHighLevelClient client;
/**
* 删除索引库
*/
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
client.indices().delete(request, RequestOptions.DEFAULT);
}
/**
* 判断索引库是否存在
*/
@Test
void existHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "索引库已经存在" : "索引库不存在");
}
/**
* 创建索引库
*/
@Test
void createHotelIndex() throws IOException {
// 1.创建request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.准备请求的参数, DSL语句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
// 3. 发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
RestClient操作文档
import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.mapper.HotelMapper;
import com.zyw.elasticsearchdemo.pojo.Hotel;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.List;
@SpringBootTest
public class ElasticsearchDemoApplicationTests1 {
private RestHighLevelClient client;
@Autowired
private HotelMapper hotelMapper;
/**
* 删除文档
*/
@Test
void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("hotel", "200216665");
client.delete(request, RequestOptions.DEFAULT);
}
/**
* 修改文档-局部更新, 全量和创建一样
*/
@Test
void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "200216665");
request.doc("price", 2600, "starName", "六钻");
client.update(request, RequestOptions.DEFAULT);
}
/**
* 查询文档
*/
@Test
void getDocument() throws IOException {
// 准备request对象
GetRequest request = new GetRequest("hotel", "200216665");
// 发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
System.out.println(hotelDoc);
}
/**
* 新增文档
*/
@Test
void addDocument() throws IOException {
// 根据id查询酒店数据
Hotel hotel = hotelMapper.selectById(200216665);
// 转换为文档对象
HotelDoc hotelDoc = new HotelDoc(hotel);
// 准备request对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
// 准备json文档
request.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON);
// 发送请求
client.index(request, RequestOptions.DEFAULT);
}
/**
* 批量导入文档
*/
@Test
void batchAddDocument() throws IOException {
List<Hotel> hotels = hotelMapper.selectList(null);
BulkRequest request = new BulkRequest();
for (Hotel hotel : hotels) {
HotelDoc hotelDoc = new HotelDoc(hotel);
request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString())
.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON));
}
// 发送
client.bulk(request, RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
DSL查询语法
分类和基本语法
全文检索查询
全文检索查询, 会对用户输入内容分词, 常用于搜索框搜索
建议把多个字段copy到一个字段里
精确查询
地理查询
复合查询
- 复合(compound)查询: 复合查询可以将其他简单查询组合起来, 实现更复杂的搜索逻辑.
-
function score
: 复分函数查询, 可以控制文档相关性算分, 控制文档排名. 例如百度竞价
-
搜索结果处理
排序
分页
高亮
默认字段要一致, 可以用require_field_match
取消一致
RestClient查询文档–高级查询
import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Map;
public class QueryDocumentTest {
private RestHighLevelClient client;
/**
* 广告置顶
*/
@Test
void adScore() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("city", "上海"));
// 算分控制
FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(boolQuery, new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAd", true),
// 分数10
ScoreFunctionBuilders.weightFactorFunction(10))
}).boostMode(CombineFunction.SUM); // 用加法 --> 分数+10
request.source().query(functionScoreQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 高亮
*/
@Test
void testHighlight() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "维也纳"));
// 高亮设置
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 排序和分页
*/
@Test
void sortAndPage() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().sort("price", SortOrder.ASC).from(20).size(5);
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 根据地理坐标排序
*/
@Test
void sortByLocation() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().sort(SortBuilders.geoDistanceSort("location",
// 坐标字符串前面是纬度,后面是经度
new GeoPoint("31.21, 121.5")).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 高亮设置
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* bool查询
* @throws IOException
*/
@Test
void testBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 添加term
boolQuery.must(QueryBuilders.termQuery("city", "上海"));
// 添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(500));
request.source().query(boolQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* bool查询 --should
* @throws IOException
*/
@Test
void testBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
shouldQuery.should(QueryBuilders.matchQuery("name", "上海")).should(QueryBuilders.matchQuery("name","北京"));
shouldQuery.minimumShouldMatch(1); // name中有上海或者北京,满足一个
boolQuery.must(shouldQuery);
// 添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(180));
request.source().query(boolQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* match查询
*/
@Test
void testMatch() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "如家"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 处理结果
* @param response
*/
private static void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
// 查询的总条数
long total = searchHits.getTotalHits().value;
System.out.println("total = " + total);
// 查询的结果数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
// 得到source
String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
// 获取高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if(!highlightFields.isEmpty()){
// 根据字段名获取高亮结果
HighlightField highlightField = highlightFields.get("name");
// 获取高亮值
String name = highlightField.getFragments()[0].toString();
// 覆盖非高亮结果
hotelDoc.setName(name);
}
// 获取location距离排序值 --> 距离4.5km
Object[] sortValues = hit.getSortValues();
if (sortValues.length != 0) {
hotelDoc.setDistance(sortValues[0]);
}
System.out.println("hotelDoc = " + hotelDoc);
}
}
/**
* 查询所有
* @throws IOException
*/
@Test
void testMatchAll() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
数据聚合
Bucket聚合
Metrics聚合
- 指定排序字段
RestClient实现聚合
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AggregationTest {
private RestHighLevelClient client;
/**
* 品牌, 城市聚合
* example --> {品牌=[7天酒店, 如家, 皇冠假日, 速8, 万怡, 华美达, 和颐, 万豪, 喜来登, 希尔顿], 城市=[上海, 北京, 深圳]}
*/
@Test
void name() throws IOException {
Map<String, List<String>> result = new HashMap<>();
SearchRequest request = new SearchRequest("hotel");
// TODO 可以增加查询条件过滤,条件和前面一样,对满足的文档进行聚合
request.source().size(0); // 去掉文档,只看聚合结果
request.source().aggregation(AggregationBuilders
.terms("brandAgg") // 名称自己定
.field("brand")
.size(10)); // 结果的前十条
request.source().aggregation(AggregationBuilders
.terms("cityAgg") // 名称自己定
.field("city")
.size(10)); // 结果的前十条
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 处理结果
Aggregations aggregations = response.getAggregations();
List<String> brandList = getAggByName(aggregations, "brandAgg");
result.put("品牌", brandList);
List<String> cityList = getAggByName(aggregations, "cityAgg");
result.put("城市", cityList);
System.out.println(result);
}
private static List<String> getAggByName(Aggregations aggregations, String aggName) {
Terms brandterms = aggregations.get(aggName);
List<? extends Terms.Bucket> buckets = brandterms.getBuckets();
List<String> list = new ArrayList<>();
for (Terms.Bucket bucket : buckets) {
list.add(bucket.getKeyAsString());
}
return list;
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
拼音分词器
github地址: https://github.com/infinilabs/analysis-pinyin
解压放到elasticsearch插件目录
重启elasticsearch
- 测试
POST /_analyze
{
"text": "我们都是祖国的花朵",
"analyzer": "pinyin"
}
自定义分词器
测试自定义分词器
GET /test/_analyze
{
"text": "java是最好的语言",
"analyzer": "my_analyzer"
}
自动补全
RestApi实现自动补全
文章来源:https://www.toymoban.com/news/detail-844094.html
- hotel索引库
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
},
"completion_analyzer": {
"tokenizer": "keyword",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart"
},
"suggestion":{
"type": "completion",
"analyzer": "completion_analyzer",
"search_analyzer": "ik_smart"
}
}
}
}
- HotelDoc.java
增加suggestion
字段文章来源地址https://www.toymoban.com/news/detail-844094.html
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Data
@NoArgsConstructor
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String location;
private String pic;
private Object distance;
private Boolean isAd;
private List<String> suggestion;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
if(this.business.contains("/")){
// business有多个值,切割
String[] arr = this.business.split("/");
this.suggestion = new ArrayList<>();
this.suggestion.add(this.brand);
Collections.addAll(this.suggestion, arr);
}else{
this.suggestion = Arrays.asList(this.brand,this.business);
}
}
}
- 查询和解析结果
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
public class SuggestionTest {
private RestHighLevelClient client;
/**
* 自动补全
*/
@Test
void test() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().suggest(new SuggestBuilder()
.addSuggestion("suggestions", // 名称自定义 解析结果时要与此保持一致
SuggestBuilders.completionSuggestion("suggestion") // HotelDoc定义的字段
.prefix("sd") // 关键字
.skipDuplicates(true).size(10))
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Suggest suggest = response.getSuggest();
CompletionSuggestion suggestions = suggest.getSuggestion("suggestions"); // 名称和上面一致
List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
for (CompletionSuggestion.Entry.Option option : options) {
String text = option.getText().toString();
System.out.println(text);
}
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
补充
- 分词
POST /_analyze
{
"text": "java是最好的语言",
"analyzer": "ik_smart"
}
- 查所有
GET /hotel/_search
到了这里,关于javaAPI操作Elasticsearch的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!