留神 这些所以的货色版本都要一样
ElasticSearch装置
jdk至多1.8 ElasticSearch客户端,界面工具
留神
jdk必须与cpu的位数是一样的 否则会报JNA谬误
下载地址
https://www.elastic.co/cn/ shearch和kibabn
https://github.com/medcl/elas... ik分词器
https://github.com/mobz/elast... head插件下载
这些都是解压可用
Elasticearch 启动
点击 bin\下的elasticsearch.bat
如果电脑内存不够大 启动前 记得去 config\jvm.options 批改
-Xms256m
-Xmx256m
拜访 http://127.0.0.1:9200/
head插件
必须要有 nodejs 因为他是前端写的
- cnpm install 装置依赖
- cnpm run start
留神如果9100端口被占用
netstat -ano | findstr "9100"
而后去工作管理器详细信息里去关
启动胜利拜访 http://localhost:9100/
9200 和 9100 是跨域的
须要在elasticearch.yaml中配置跨域
http.cors.enabled: truehttp.cors.allow-origin: "*"
而后重启es服务
head就相当于navicat
kibana
这也是一个规范的前端化工程
bin\kibana.bat
http://localhost:5601
找到开发工具
汉化!!!!
改配置kibana.yaml
i18n.locale: "zh-CN"
IK分词器(中文) elasticsreach默认的不是中文的
放在elasticsreach中的plugins中
有两种算法
- ik_smart 默认分词器 他所能了解 不反复的单词 或者字
ik_max_word 最细粒度 穷进词库可能
GET _analyze{ "analyzer": "ik_smart", "text": "我超级喜爱多帅哦"}GET _analyze{ "analyzer": "ik_max_word", "text": "我超级喜爱多帅哦"}
你本人须要的词,须要本人加到字典中
IKAnalyzer.cfg.xml编写本人的kb.dic而后在配置文件中增加<entry key="ext_dict">kb.dic</entry>重启es
Rest格调
1、创立一个索引 随到在申请体中插入数据
PUT /索引名/类型名(可无)/文档id{ json申请体}
增加
创立一个索引,指定字段类型 然而没有插入信息
PUT /test2{ "mappings": { "properties": { "name":{ "type": "text" }, "age": { "type": "long" }, "birthday":{ "type": "date" } } }}
取得信息
GET test2
如果不写就是默认类型 _doc keyword是不可分割的
PUT /test3/_doc/1{ "name": "多帅哦", "age": 18, "birthday": "1999-10-20"}
GET _cat/health 数据库状态GET _cat/indices?v 数据库信息
批改
1、间接暴力PUT
PUT /test3/_doc/1{ "name": "多帅哦123", "age": 18, "birthday": "1999-10-20"}然而索引信息会扭转 版本啊 状态啊
2、POST
POST /test3/_doc/1/_update{ "doc":{ "name":"shuaikb" }}
3、删除索引
DELETE test1 依据你的申请是删除什么 索引或者文档
条件查问
GET /test3/_search?q=name:多帅哦/"_score" : 0.5753642,这个_score是匹配度 匹配度越高分数越高
简单搜寻
GET /test3/_search{ "query": { "match": { "name": "多帅" } }}name里蕴含 多帅 的都会进去"hits"外面蕴含了 索引的信息 和查问的后果GET /test3/_search{ "query": { "match": { "name": "多帅" } }, "_source": ["name","age"]}查问数据中只显示 name和age当前在java中操作es 所有的办法都是这里的key排序"sort": [ { "age": { "order": "desc" } } ]通过什么字段进行排序 desc降序 asc升序分页"from": 0, 第几个数据开始"size": 1 返回多少条
布尔值查问
GET /test3/_search{ "query": { "bool": { "must": [ { "match": { "name": "多帅" } }, { "match":{ "age": 18 } } ] } }}准确匹配 多条件查问 must 相当于 and should 想当于 ornot 相当于 !=GET /test3/_search{ "query": { "bool": { "must": [ { "match": { "name": "多帅" } } ], "filter": [ { "range": { "age": { "gte": 10, "lte": 17 } } } ] } }}filter 能够进行数据过滤 gt> gte>= lt< lte<= eq
GET /test3/_search{ "query": { "match": { "tag": "男 技术" } }}tag查问的多个属性 用空格隔开term,查问时通过倒排是索引进行准确查问match,会应用分词器解析 (先剖析文档,而后通过剖析文档进行查问!)
两个类型
- text 能被分词器解析
- keyword 不能被分词器解析
GET _analyze{ "analyzer": "standard", "text": "多帅哦Shuaikb name1"}只有分词器不是keyword就不会被拆分GET testdb/_search{ "query": { "term": { "desc": { "value": "多帅哦Shuaikb desc" } } }}准确匹配 keyword类型的字段不会被分词器解析你会发现 多帅哦Shuaikb desc2不会被查问进去
多个值匹配的准确查问
GET testdb/_search{ "query": { "bool": { "should": [ { "term": { "t1": { "value": "22" } } }, { "term": { "t1": { "value": "33" } } } ] } }}
高亮查问
GET testdb/_search{ "query": { "match": { "name": "帅" } }, "highlight": { "fields": { "name": {} } }}"多<em>帅</em>哦Shuaikb name2"这个<em>就是高亮的html自定义搜寻高亮条件GET testdb/_search{ "query": { "match": { "name": "帅" } }, "highlight": { "pre_tags": "<p class='key' style='color:red'>", "post_tags": "</p>", "fields": { "name": {} } }}
Spirngboot集成es
原生依赖<repositories> <repository> <id>es-snapshots</id> <name>elasticsearch snapshot repo</name> <url>https://snapshots.elastic.co/maven/</url> </repository></repositories>理论咱们导入的依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>留神依赖的版本必须要和本人的本地的es版本统一所以须要自定义版本依赖初始化RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")));用完记得关client.close();@Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client =new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http"))); return client; }
API
索引操作
@Test public void CreateIndex() throws IOException { //创立索引 CreateIndexRequest requst = new CreateIndexRequest("shuaikb_index"); //执行申请 取得响应 CreateIndexResponse createIndexResponse = client.indices().create(requst, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } @Test void testExisIndex() throws IOException { //判断存在 GetIndexRequest request =new GetIndexRequest("shuaikb_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } @Test void testDeleteIndex() throws IOException { //删除索引 DeleteIndexRequest request =new DeleteIndexRequest("shuaikb_index"); AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete); }
文档操作
@Test void testAddDocument() throws IOException { //增加文档 Dog dog = new Dog(); dog.setName("多帅哦"); dog.setAge(12); IndexRequest requst = new IndexRequest("shuaikb_index"); //规定 PUT /shuaikb_index/_doc/1 requst.id("1"); requst.timeout(TimeValue.timeValueSeconds(1)); requst.timeout("1s"); //将咱们的数据放入申请 json requst.source(JSON.toJSONString(dog), XContentType.JSON); //客户端发送申请 IndexResponse indexResponse = client.index(requst, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); System.out.println(indexResponse.status()); } @Test void testExisDocument() throws IOException { //判断文档存在 GetRequest getRequest = new GetRequest("shuaikb_index","1"); //不获取返回的_source的上下文 getRequest.fetchSourceContext(new FetchSourceContext(false)); getRequest.storedFields("_noe_"); boolean exists = client.exists(getRequest, RequestOptions.DEFAULT); System.out.println(exists); } @Test void testGetDocument() throws IOException { //获取文档信息 GetRequest getRequest = new GetRequest("shuaikb_index","1"); GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); System.out.println(documentFields); } @Test void testUpdateDocument() throws IOException { //更新文档信息 UpdateRequest updateRequest = new UpdateRequest("shuaikb_index","1"); updateRequest.timeout("1s"); Dog dog = new Dog("你怎么说",20); updateRequest.doc(JSON.toJSONString(dog),XContentType.JSON); client.update(updateRequest,RequestOptions.DEFAULT); } @Test void testDeleteDocument() throws IOException { //删除文档信息 DeleteRequest deleteRequest = new DeleteRequest("shuaikb_index","1"); deleteRequest.timeout("1s"); DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete); }
大量数据操作
//大量数据操作 @Test void testBulkRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<Dog> dogArrayList = new ArrayList<Dog>(); dogArrayList.add(new Dog("test1",1)); dogArrayList.add(new Dog("test2",1)); dogArrayList.add(new Dog("test3",1)); dogArrayList.add(new Dog("test4",1)); dogArrayList.add(new Dog("shuaikb1",1)); dogArrayList.add(new Dog("shuaikb2",1)); dogArrayList.add(new Dog("shuaikb3",1)); dogArrayList.add(new Dog("shuaikb4",1)); for (int i = 0; i <dogArrayList.size() ; i++) { bulkRequest.add(new IndexRequest("shuaikb_index") .id(""+(i+1))//不指定id就会随机生成一个简单id .source(JSON.toJSONString(dogArrayList.get(i)),XContentType.JSON)); } BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.hasFailures());//返回false代表胜利 } //查问 //searchRequest 搜寻申请 //searchSourceBuilder 条件结构 // HighlightBuilder 结构高亮 //MatchAllQueryBuilders 全副查问 //TermQueryBuilder 准确查问 @Test void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest("shuaikb_index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //准确 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "test1"); //全副 //MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.from(); searchSourceBuilder.size(); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(searchSourceBuilder); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(search)); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } }