一、几个概念
1、Index Type Document
一般我们初学时会把这些与数据库进行对照方便理解
- Index->Database
- Type->Table (最新版本已经不使用Type了,所以很多人会奇怪为什么去掉了?ES并非和数据库是相同的,所以不要完全按数据库的方式来看ES)
- Document->Row
2、倒排索引
参考此文:(一般我们从目录找到相应的文章为正向索引,如果从关键词索引找到对应的文章即倒排索引)
二、几种Java调用ES方式
- Rest API Test
- Using Spring Data Repositories
- Using the RestClient
- Using Transport Client
三、Rest API Test
参考官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-index.html
#1、创建索引及document #e.g. PUT /index名/_doc/document id #最新版本已经没有type,且这里固定为_doc PUT /index-test/_doc/1 { "id":"1", "name": "hello world", "code":11 } #2、更新document(有两种方式:方式一与创建索引一致就会更新;方式二改成POST) #e.g. POST /index名/_doc/document id POST /index-test/_doc/1 { "id":"1", "name": "hello world", "code":12 } #3、查询 #e.g. GET /index名/_search #可以查询出所以索引及数据 #其中_score为匹配度 GET /index-test/_search { "query": { "match_all": {} } } #4、模糊查询 关键词 match match_phrase match_phrase_prefix #match 包含模糊匹配、单词匹配、短语匹配 #e.g. 查询“hello w”有结果 w不是一个完全的单词或短语 #match_phrase 包含单词匹配、短语匹配 #e.g. 查询“hello w”无结果 #match_phrase_prefix 包含单词匹配、短语匹配,同时增加在集合内通配符匹配 #e.g. 查询“hello w”有结果 #match match_phrase_prefix 区别在于 hello wadasas match有结果, #match_phrase_prefix无结果 GET /index-test/_search { "query": { "match": { "name": "hello wa" } } } #5、精确查询(精确是指精确到单词) GET /index-test/_search { "query": { "term": { "name": "hello" } } } #6、精确查询,且使用多个单词(精确是指精确到单词) GET /index-test/_search { "query": { "terms": { "name": ["hello","world"] } } } #7、增加中文分词ik #支持2种分词模式ik_smart ik_max_word #测试分词效果 _analyze GET /index-test/_analyze { "analyzer": "ik_max_word", "text": "你世界好" } #8、排序与分页 GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "age": "asc" } ], "from": 1, "size": 1 } #9、词频统计 DELETE message_index #创建索引数据结构 PUT message_index { "mappings": { "properties":{ "message": { "analyzer": "ik_smart", "type": "text", "fielddata":"true" } } } } #增加doc1 PUT /message_index/_doc/1 { "message":"最新版本已经不使用Type了,所以很多人会奇怪为什么去掉了" } #增加doc2 PUT /message_index/_doc/2 { "message":"ES并非和数据库是相同的,所以不要完全按数据库的方式来看ES" } #aggs为Aggregations(聚合)缩写 #size 10 为前10的统计结果 #默认热点降序出结果 POST /message_index/_search { "size" : 0, "aggs" : { "messages" : { "terms" : { "size" : 10, "field" : "message" } } } }
最新Java Elasticsearch 7.10教程(汇总)
玄明Hanko:最新Java Elasticsearch 7.10教程(汇总)