Elaticsearch,简称为ES,ES是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理 PB 级别(大数据时代)的数据。ES由 Java 语言开发并使用 Lucene 作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTFULL API 来隐藏 Lucene 的复杂性,从而让全文搜索变得简单。据国际权威的数据库产品评测机构 DB Engines 的统计,在2016 年1月,ElasticSearch 已超过 Solr 等,成为排名第一的搜索引擎类应用。
Kibana可以将es的数据通过友好的页面展示出来 ,提供实时分析的功能。
ik分词器增加自己的配置
基本Rest风格说明
PUT命令
PUT
失败(超时)解决办法:先把es下的jvm.options里的两个1g都改成2g,然后把kibana里的配置文件请求时间扩大一倍
<properties>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
<es.version>7.9.1es.version>
properties>
<dependencies>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>${es.version}version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>${es.version}version>
<exclusions>
<exclusion>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.elasticsearch.plugingroupId>
<artifactId>transport-netty4-clientartifactId>
<version>${es.version}version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-clientartifactId>
<version>${es.version}version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>${es.version}version>
<exclusions>
<exclusion>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
exclusion>
<exclusion>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-clientartifactId>
exclusion>
exclusions>
dependency>
dependencies>
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
)
);
return client;
}
}
@Test
void testCreateIndex() throws IOException {
//创建索引请求
CreateIndexRequest request = new CreateIndexRequest("hzh_index");
//客户端请求
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hzh_index");
boolean exist = client.indices().exists(request,RequestOptions.DEFAULT);
System.out.println(exist);
}
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hzh_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
}
@Test
void testAddDocument() throws IOException {
//创建对象
User user = new User("HZH",3);
IndexRequest request = new IndexRequest("hzh_index");
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));//一秒钟
request.timeout("1s");
//将我们的数据放入json
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());//
System.out.println(indexResponse.status()); //对应我们命令返回的状态
}
@Test
void testIsExist() throws IOException {
GetRequest getRequest = new GetRequest("hzh_index","dK6jnXoBhRewEAABR0QT");
//不获取返回值的上下文
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("hzh_index", "1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString()); //打印文档的内容
System.out.println(getResponse); //打印全部内容
}
@Test
void testUpdateDocument() throws IOException{
UpdateRequest updateRequest = new UpdateRequest("hzh_index","1");
updateRequest.timeout("1s");
User user = new User("HZH2", 5);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
@Test
void testDeleteDocument() throws IOException{
DeleteRequest deleteRequest = new DeleteRequest("hzh_index", "1");
deleteRequest.timeout("1s");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());
}
@Test
void testBulkRequest() throws IOException{
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
List<User> lists = new ArrayList<>();
for(int i=0;i<=20;i++){
lists.add(new User("hzh",i));
}
//批处理请求
for(int i=0;i<lists.size();i++){
bulkRequest.add(
new IndexRequest("hzh_index")
.id(""+(i+1))
.source(JSON.toJSONString(lists.get(i)),XContentType.JSON)
);
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());
}
@Test
void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest("hzh_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//精确匹配
//TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "hzh");
//匹配所有
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(matchAllQueryBuilder);
//分页
searchSourceBuilder.from(0);
searchSourceBuilder.size(1);
searchSourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("-----------------------");
int index = 0;
for(SearchHit documentFields:searchResponse.getHits()){
index++;
System.out.println(documentFields.getSourceAsString()+"\n");
}
System.out.println("index = "+index);
}
@Test
void testFindAllIndex() throws IOException {
GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
getAliasesRequest.masterNodeTimeout(new TimeValue(60,TimeUnit.SECONDS));
GetAliasesResponse alias = client.indices().getAlias(getAliasesRequest,RequestOptions.DEFAULT);
Map<String, Set<AliasMetadata>> aliases = alias.getAliases();
for (String key : aliases.keySet()) {
System.out.println("Key = " + key);
}
}
@Test
void takeJsonStr() throws IOException {
SearchRequest searchRequest = new SearchRequest("hzh_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//精确匹配
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("_id", "-322inoBKIkXtm6iIwBi");
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("name","*魂*");
//匹配所有
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
//searchSourceBuilder.query(matchAllQueryBuilder);
searchSourceBuilder.query(wildcardQueryBuilder);
searchSourceBuilder.sort("_id",SortOrder.DESC);
//分页
searchSourceBuilder.from(0);
searchSourceBuilder.size(3);
searchSourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
int index = 0;
for(SearchHit documentFields:searchResponse.getHits()){
String oriStr = JSON.toJSONString(searchResponse.getHits());
JSONObject strToJson = JSONObject.parseObject(oriStr);
System.out.println(strToJson);
String strId = strToJson.getJSONArray("hits").getJSONObject(0).getString("id");
System.out.println("d-----------------------");
System.out.println("strId: " + strId);
JSONObject json = JSONObject.parseObject(documentFields.getSourceAsString());
//System.out.println(json);
System.out.println(json.getString("name"));
System.out.println(json.getString("en_name"));
System.out.println(json.getJSONArray("nodes").toJSONString());
System.out.println("u-----------------------");
index++;
}
System.out.println("index = "+index);
}