流行的搜索引擎
介绍
索引 index
一个索引可以理解成一个关系数据库的库
类型 type
(es7已经不推荐使用)
一种type就像一类表,比如user表,order表
映射 mapping
(es7已经不推荐使用)
mapping定义了每个字段的类型等信息。相当于关系型数据库中的表结
文档 document
一个document相当于关系型数据库中的一⾏记录
字段 field
相当于关系型数据库表的字段
集群 cluster
-集群由一个或多个节点组成,一个集群有一个默认名称elasticsearch
节点 node
集群的节点,一台机器 或者一个进程
分片和副本 node
副本是分片的副本。分⽚有主分片(primary Shard)和副本分片(replica Shard)之分。Index数据在物理理上被分布在多个主分片中,每个主分片只存放部分数据。每个主分⽚可以有多个副本,叫副本分片,是主分片的复制
核心数据类型
字符串
text
用于全文索引,该类型的字段将通过分词器 进⾏分词
keyword
不分词,只能搜索该字段的完整的值
数值型
integer, short, byte, double, float, half_float, scaled_float
数值型 boolean
二进制
binary
该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索
范围类型
范围类型表示值是一个范围,而不是⼀个具体的值
integer_range, float_range, long_range, double_range, date_range
譬如 age 的类型是 integer_range,那么值可以是 {“gte” : 20, “lte” : 40};搜索 “term” : {“age”: 21} 可以搜索该值
日期
由于Json没有date类型,所以es通过识别字符串是否符合format定义的格式来判断是否为date类型
format默认为:strict_date_optional_time||epoch_millis,”2022-01-01” “2022/01/01 12:10:30” 这种字符串格式
搜索
Query DSL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| GET bank/_search
{ "query":{ "match_all": {},
"match": { "field_one": "今天 吃好饱" },
"match_phrase": { "field_one": "今天 吃好饱" },
"multi_match": { "query": "今天 吃好饱", "field": ["field_one","field_two"] },
"bool": { "mush": [ { "match": { "field_one": "今天 吃好饱" }, }, { "match": { "field_two": "今天 吃好饱" }, } ], "mush_not": [ { "match": { "field_three": "今天 吃好饱" }, } ], "should": [ { "match": { "field_four": "今天 吃好饱" }, }, ], "filter":{ "range": { "age": { "gte": 18, "lte": 30 } } } },
"nested":{ "path":"addr", "query":{ "term": { "age": "28" } } },
"term": { "age": "28" },
"range": { "age": { "gte": 10, "lte": 20 } } }, "sort": [ { "id": "desc" } ], "form": 0, "size": 10, "_source": ["field_1","field_2"] }
|
Aggregations 聚合查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| GET bank/_search
{ "query": { "match_all": {}, }, "aggs": { "ageAgg": { "terms": { "field": "age", "size": 10 }, "aggs": { "ageAvg": { "avg": { "field": "banlance", } } } } } }
|
安装
Docker安装
SpringBoot使用
es原生框架组件
pom
1 2 3 4 5 6 7 8 9
| <properties> <elasticsearch.version>7.6.2</elasticsearch.version> </properties> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.6.2</version> </dependency>
|
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Configuration public class MyElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); COMMON_OPTIONS = builder.build(); }
@Bean public RestHighLevelClient restHighLevelClient() { return new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1", 9200, "http"), new HttpHost("127.0.0.1", 9201, "http") ) ); } }
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @Autowired private RestHighLevelClient restHighLevelClient;
@Test public void esTest() throws IOException { IndexRequest users = new IndexRequest("users"); users.id("1"); Object o = new Object(); users.source(JSONObject.toJSONString(o), XContentType.JSON); IndexResponse response = restHighLevelClient.index(users, MyElasticSearchConfig.COMMON_OPTIONS); }
@Test public void esSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("users"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query( QueryBuilders.matchQuery("name","张三") ); sourceBuilder.aggregation( AggregationBuilders.terms("aggInfo").field("age").size(10) ); searchRequest.source(sourceBuilder); SearchResponse response = restHighLevelClient.search(searchRequest, MyElasticSearchConfig.COMMON_OPTIONS);
SearchHits hits = response.getHits(); SearchHit[] hitsHits = hits.getHits(); for (SearchHit hitsHit : hitsHits) { Object bean = JSONObject.parseObject(hitsHit.getSourceAsString(), Object.class); System.out.println(bean); } }
|
spring-boot-data-elasticsearch
注:只有springboot2.3.x以上的版本支持es7
pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> </dependencies>
|
yml
1 2 3 4 5 6 7 8 9 10 11
| spring: application: name: TestElasticsearch elasticsearch: rest: uris: 192.168.0.18:9200 username: elastic password: elastic
|
实体
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Document(indexName = "user-index",replicas = 3) public class User implements Serializable {
private static final long serialVersionUID = 2852000052380434205L;
@Id @Field(type = FieldType.Long, store = true) private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word", store = true) private String name;
|
继承ElasticsearchRepository
1 2 3
| public interface UserRepository extends ElasticsearchRepository<User,Long> { }
|
集群搭建
配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13
|
cluster.name: es-1
network.host: 0.0.0.0
node.name: node-1
http.port: 9201
transport.tcp.port: 9301
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9301","127.0.0.1:9302","127.0.0.1:9303"]
|
相关文章
数据库连接池
SpringIOC
Junit和Spring
Tomcat
Servlet
Request,Response和ServletContext
Cookie和Session
JSP和EL和Jstl
Filter和Listener
Mybatis