elasticsearch基本使用
一、安装
1、版本对应
2、elasticsearch
链接:https://pan.baidu.com/s/1Tsjff_phJVnsup5sC4BnWw
提取码:idhn
3、视图工具 Elasticsearch-head
elasticsearchheadmaster1.zip
4、Ik分词器
elasticsearchanalysisik5.x.zip
配置运行
进入Elasticsearch安装目录下的config目录,修改elasticsearch.yml文件.在文件的末尾加入以下代码
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
修改elasticsearch-head-master\Gruntfile.js 在connect.server下加上hostname:’*’
然后在plugins目录下放入分词插件
安装head需要安装node和grunt
https://nodejs.org/en/download/
**npm install -g grunt-cli **
打开cmd命令行窗口 ,在D:\environment\elasticsearch-head-master 下执行npm install 安装
,完成后执行grunt server 或者npm run start 运行head插件
打开浏览器访问:http://127.0.0.1:9100
二、springboot集成
1、pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nick</groupId>
<artifactId>es-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>es-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- es包配置,es5.5.0需要排除高版本依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.0.9.RELEASE</version>
<exclusions>
<!-- 排除高版本的包 -->
<exclusion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入5.5.0的包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-cli</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、增删改测试代码
可以使用ElasticsearchTemplate
,ElasticsearchRepository
,和TransportClient
操作数据
package com.example.elasticsearch.demlelasticsearch;
import com.example.elasticsearch.demlelasticsearch.model.Item;
import com.example.elasticsearch.demlelasticsearch.model.SpiderInfo;
import com.example.elasticsearch.demlelasticsearch.service.ElasticSearchService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.join.query.HasParentQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ScrolledPage;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.web.PageableDefault;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemlElasticsearchApplicationTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ElasticSearchService esService;
@Autowired
private TransportClient client;
/**
* @Description:创建索引,会根据Item类的@Document注解信息来创建
* @Author: https://blog.csdn.net/chen_2890
* @Date: 2018/9/29 0:51
*/
@Test
public void testCreateIndex() {
elasticsearchTemplate.createIndex(Item.class);
elasticsearchTemplate.putMapping(Item.class);
}
@Test
public void demo() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// QueryBuilders构造查询条件
// rangeQuery搜索范围,第二个参数表示包含该时间点
QueryBuilder queryBuilder = null;
try {
queryBuilder = QueryBuilders.rangeQuery("id")
.from(sdf.parse("2017-08-03").getTime(), true)
.to(sdf.parse("2019-08-22").getTime(), true);
} catch (ParseException e) {
e.printStackTrace();
}
// FieldSortBuilder构造排序条件
SortBuilder sortBuilder = new FieldSortBuilder("id").order(SortOrder.DESC)
// 调用unmappedType方法避免无数据时报错
.unmappedType("long");
// NativeSearchQueryBuilder构造Query对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withSort(sortBuilder)
.build();
// 查询
List<Item> list = elasticsearchTemplate.queryForList(searchQuery, Item.class);
System.out.println(list);
}
@Test
public void demo2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 构建筛选条件
QueryBuilder queryBuilder = null;
try {
queryBuilder = QueryBuilders.rangeQuery("datetime")
.from(sdf.parse("2019-04-03").getTime(), true)
.to(sdf.parse("2019-04-04").getTime(), true);
} catch (ParseException e) {
e.printStackTrace();
}
// 构建排序
SortBuilder sortBuilder = new FieldSortBuilder("datetime").order(SortOrder.DESC)
// 调用unmappedType方法避免无数据时报错
.unmappedType("long");
// 构建分页(可选)
Pageable pageable = PageRequest.of(0, 10);
// 构造Query对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withSort(sortBuilder)
.withPageable(pageable)
.build();
// 查询
Page<Item> page = esService.search(searchQuery);
System.out.println(page);
}
@Test
public void demo3() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 构建筛选条件
QueryBuilder queryBuilder = null;
try {
queryBuilder = QueryBuilders.rangeQuery("datetime")
.from(sdf.parse("2019-04-03").getTime(), true)
.to(sdf.parse("2019-04-04").getTime(), true);
} catch (ParseException e) {
e.printStackTrace();
}
// 构建排序条件
SortBuilder sortBuilder = new FieldSortBuilder("datetime").order(SortOrder.DESC)
// 调用unmappedType方法避免无数据时报错
.unmappedType("long");
// 查询,指定索引、类型、查询条件、排序条件
SearchResponse searchResponse = client.prepareSearch("spiderinfo").setTypes("spiderinfo")
.setQuery(queryBuilder)
.addSort(sortBuilder)
.get();
// 自行获取结果
SearchHits hits = searchResponse.getHits();
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
// 遍历对象
SearchHit searchHit = iterator.next();
String doc = searchHit.getSourceAsString();
System.out.println("doc" + doc);
}
}
@Test
public void addData() {
Item item = new Item();
item.setId(2L);
item.setBrand("南华山");
item.setCategory("aa");
item.setImages("c.jpg");
item.setPrice(31D);
item.setTitle("青山石");
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(item.getId().toString())
.withObject(item)
.build();
elasticsearchTemplate.index(indexQuery);
}
@Test
public void demo4() {
// 创建查询条件对象
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// 拼接查询条件 是精确搜索,对检索词不分词
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "test");
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery("山").defaultField("title");
queryBuilder.must(queryStringQueryBuilder); //假设查询状态为1的
// 创建查询对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices("item")//索引名
.withTypes("docs")//类型名
.withQuery(queryBuilder)// 查询条件对象
.withPageable(PageRequest.of(0, 10))//从0页开始查,每页10个结果
.build();
// 滚动查询
ScrolledPage<Item> scroll = (ScrolledPage<Item>) elasticsearchTemplate.startScroll(3000, searchQuery, Item.class);
// 判断是否有内容
while (scroll.hasContent()) {
List<Item> content = scroll.getContent();
// 业务逻辑省略
//取下一页,scrollId在es服务器上可能会发生变化,需要用最新的。发起continueScroll请求会重新刷新快照保留时间
scroll = (ScrolledPage<Item>) elasticsearchTemplate.continueScroll(scroll.getScrollId(), 3000, Item.class);
System.out.println("内容:" + scroll);
}
// 最后释放查询
elasticsearchTemplate.clearScroll(scroll.getScrollId());
}
@Test
public void deleteIndex() {
//删除索引
boolean result = elasticsearchTemplate.deleteIndex("item");
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "test");
//boolean result = elasticsearchTemplate.delete();
}
@Test
public void deleteObjectsOfAsset() {
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.idsQuery().addIds("1"));
deleteQuery.setIndex("item");
deleteQuery.setType("docs");
elasticsearchTemplate.delete(deleteQuery);
}
}