diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 6b3332d..0000000 --- a/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM java:8 -EXPOSE 8080 -MAINTAINER ylanhds@gmail.com -VOLUME /tmp -# 将主机环境的jar包,以文件名.jar添加到docker镜像中。 -ADD ./target/spring-elasticsearch-1.0.0.jar spring-elasticsearch.jar -RUN sh -c 'touch /spring-elasticsearch.jar' -ENV JAVA_OPTS="" -#启动容器之后,默认的运行命令 -ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /spring-elasticsearch.jar" ] diff --git a/Dockerfile.sh b/Dockerfile.sh deleted file mode 100755 index c5e2d98..0000000 --- a/Dockerfile.sh +++ /dev/null @@ -1,7 +0,0 @@ -#! /bin/bash -cd $(dirname "${BASH_SOURCE[0]}") && pwd -mvn clean -mvn package -docker build -t spring-elasticsearch . -docker run -d --name spring-elasticsearch -p 8080:8080 spring-elasticsearch - diff --git a/pom.xml b/pom.xml index f3bb1d6..e643c33 100644 --- a/pom.xml +++ b/pom.xml @@ -9,48 +9,27 @@ com.fablen - spring-elasticsearch + spring-elasticsearch-7.8.0 1.0.0 - spring-elasticsearch + spring-elasticsearch-7.8.0 Elasticsearch project for Spring Boot UTF-8 UTF-8 - 1.8 - 7.6.2 - 1.2.3 + 11 + 7.8.0 - org.springframework.boot - spring-boot-starter-data-elasticsearch + com.alibaba + fastjson + 1.2.72 org.springframework.boot - spring-boot-starter-logging - - - ch.qos.logback - logback-core - ${ch.qos.logback.version} - - - ch.qos.logback - logback-classic - ${ch.qos.logback.version} - - - ch.qos.logback - logback-access - ${ch.qos.logback.version} - - - - net.logstash.logback - logstash-logback-encoder - 6.3 + spring-boot-starter-data-elasticsearch org.springframework.boot diff --git a/src/main/java/com/fablen/elasticsearch/config/ElasticSearchClientConfig.java b/src/main/java/com/fablen/elasticsearch/config/ElasticSearchClientConfig.java deleted file mode 100644 index 083eebf..0000000 --- a/src/main/java/com/fablen/elasticsearch/config/ElasticSearchClientConfig.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fablen.elasticsearch.config; - -import org.elasticsearch.client.RestHighLevelClient; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.elasticsearch.client.ClientConfiguration; -import org.springframework.data.elasticsearch.client.RestClients; -import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; -/** - * - * @author zhangbaosheng - */ -@Configuration -public class ElasticSearchClientConfig extends AbstractElasticsearchConfiguration { - - @Bean - @Override - public RestHighLevelClient elasticsearchClient() { - - final ClientConfiguration clientConfiguration = ClientConfiguration.builder() - .connectedTo("122.152.213.179:9200") - .build(); - - return RestClients.create(clientConfiguration).rest(); - } -} diff --git a/src/main/java/com/fablen/elasticsearch/config/ElasticSearchConfig.java b/src/main/java/com/fablen/elasticsearch/config/ElasticSearchConfig.java new file mode 100644 index 0000000..c521198 --- /dev/null +++ b/src/main/java/com/fablen/elasticsearch/config/ElasticSearchConfig.java @@ -0,0 +1,27 @@ +package com.fablen.elasticsearch.config; + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *

+ * Description: + *

+ * + * @author ZhangBaoSheng + * @version v1.0.0 + */ +@Configuration +public class ElasticSearchConfig { + @Bean + public RestHighLevelClient restHighLevelClient() { + return new RestHighLevelClient( + RestClient.builder( + new HttpHost("122.152.213.179", 9200, "http") + ) + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/fablen/elasticsearch/controller/BookController.java b/src/main/java/com/fablen/elasticsearch/controller/BookController.java deleted file mode 100644 index c661b6d..0000000 --- a/src/main/java/com/fablen/elasticsearch/controller/BookController.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.fablen.elasticsearch.controller; - -import com.fablen.elasticsearch.entity.Book; -import com.fablen.elasticsearch.repository.BookRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author zhangbaosheng - */ -@RestController -public class BookController { - // 定义一个全局的记录器,通过LoggerFactory获取 - private final static Logger log = LoggerFactory.getLogger(BookController.class); - @Autowired - BookRepository bookRepository; - - @GetMapping("/book") - public Iterable hello() { - Iterable all = bookRepository.findAll(); - log.info("返回数据 "+all); - return bookRepository.findAll(); - } -} diff --git a/src/main/java/com/fablen/elasticsearch/entity/Book.java b/src/main/java/com/fablen/elasticsearch/entity/Book.java index f831821..8c60413 100644 --- a/src/main/java/com/fablen/elasticsearch/entity/Book.java +++ b/src/main/java/com/fablen/elasticsearch/entity/Book.java @@ -1,16 +1,25 @@ package com.fablen.elasticsearch.entity; import lombok.Data; -import org.springframework.data.elasticsearch.annotations.Document; -import org.springframework.data.elasticsearch.annotations.Field; /** - * @author zhangbaosheng + *

+ * Description: + *

+ * + * @author ZhangBaoSheng + * @version v1.0.0 */ @Data -@Document(indexName = "index_book") public class Book { - @Field - private int id; - private String name; + private String bookName; + private int bookPrice; + + public Book() { + } + + public Book(String name, int price) { + this.bookName = name; + this.bookPrice = price; + } } diff --git a/src/main/java/com/fablen/elasticsearch/repository/BookRepository.java b/src/main/java/com/fablen/elasticsearch/repository/BookRepository.java deleted file mode 100644 index ecd950a..0000000 --- a/src/main/java/com/fablen/elasticsearch/repository/BookRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.fablen.elasticsearch.repository; - - -import com.fablen.elasticsearch.entity.Book; -import org.springframework.data.repository.CrudRepository; - -/** - * @author zhangbaosheng - */ -public interface BookRepository extends CrudRepository { - -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index cfbd7bb..da617fa 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,2 @@ spring.application.name= ELASTICSEARCH-TEST -server.port= 8080 - +server.port= 8080 \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml deleted file mode 100644 index b9c7e06..0000000 --- a/src/main/resources/logback-spring.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - INFO - - - - ${CONSOLE_LOG_PATTERN} - utf8 - - - - - - www.fablen.com:5044 - - - - - - - - - diff --git a/src/test/java/com/fablen/elasticsearch/ElasticsearchApplicationTests.java b/src/test/java/com/fablen/elasticsearch/ElasticsearchApplicationTests.java new file mode 100644 index 0000000..196be4e --- /dev/null +++ b/src/test/java/com/fablen/elasticsearch/ElasticsearchApplicationTests.java @@ -0,0 +1,157 @@ +package com.fablen.elasticsearch; + +import com.alibaba.fastjson.JSON; +import com.fablen.elasticsearch.entity.Book; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.bulk.BulkResponse; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.IOException; +import java.util.ArrayList; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ElasticsearchApplicationTests { + + + @Autowired + private RestHighLevelClient restHighLevelClient; + + //创建索引 + @Test + public void testCreateIndex() throws IOException { + CreateIndexRequest createIndexRequest = new CreateIndexRequest("book_wenxue"); + CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); + System.out.println(createIndexResponse); + } + + /** + * 测试索引是否存在 + * + * @throws IOException + */ + @Test + public void testExistIndex() throws IOException { + GetIndexRequest request = new GetIndexRequest("book_wenxue"); + boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); + System.out.println(exists); + } + + /** + * 删除索引 + */ + @Test + public void deleteIndex() throws IOException { + DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("book_wenxue"); + AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); + System.out.println(delete.isAcknowledged()); + } + + /** + * 测试添加文档 + * + * @throws IOException + */ + @Test + public void createDocument() throws IOException { + Book book = new Book("三国演义", 188); + IndexRequest request = new IndexRequest("book_wenxue"); + request.id("1"); + request.timeout(TimeValue.timeValueSeconds(1)); + request.timeout("1s"); + //将我们的数据放入请求,json + request.source(JSON.toJSONString(book), XContentType.JSON); + //客服端发送请求 + IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); + System.out.println(index.toString()); + //对应我们的命令返回状态 + System.out.println(index.status()); + } + + //判断是否存在文档 + @Test + public void testIsExist() throws IOException { + GetRequest getRequest = new GetRequest("book_wenxue", "1"); + //不获取返回的source的上下文 + getRequest.fetchSourceContext(new FetchSourceContext(false)); + getRequest.storedFields("_none_"); + boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT); + System.out.println(exists); + } + + //获取文档信息 + @Test + public void testGetDocument() throws IOException { + GetRequest getRequest = new GetRequest("book_wenxue", "1"); + GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); + //打印文档信息 + System.out.println(response.getSourceAsString()); + System.out.println(response); + } + + //更新文档信息 + @Test + public void testUpdateDocument() throws IOException { + UpdateRequest request = new UpdateRequest("book_wenxue", "1"); + request.timeout("1s"); + Book book = new Book("三国", 333); + request.doc(JSON.toJSONString(book), XContentType.JSON); + UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT); + System.out.println(update); + System.out.println(update.status()); + } + + //删除文档 + @Test + public void testDeleteDocument() throws IOException { + DeleteRequest request = new DeleteRequest("book_wenxue", "1"); + request.timeout("10s"); + DeleteResponse update = restHighLevelClient.delete(request, RequestOptions.DEFAULT); + System.out.println(update.status()); + } + + //批量插入数据 + @Test + public void testBulkRequest() throws IOException { + BulkRequest bulkRequest = new BulkRequest(); + bulkRequest.timeout("10s"); + ArrayList books = new ArrayList<>(); + books.add(new Book("三国演义", 1)); + books.add(new Book("红楼梦", 12)); + books.add(new Book("西游记", 13)); + books.add(new Book("水浒传", 14)); + books.add(new Book("哥德巴赫猜想", 15)); + for (int i = 0; i < books.size(); i++) { + bulkRequest.add( + new IndexRequest("book_wenxue") + .id("" + i + 1) + .source(JSON.toJSONString(books.get(i)), XContentType.JSON) + ); + } + BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); + System.out.println(bulk); + + } +} \ No newline at end of file diff --git a/src/test/java/com/fablen/elasticsearch/ElasticsearchTests.java b/src/test/java/com/fablen/elasticsearch/ElasticsearchTests.java deleted file mode 100644 index 90afb1b..0000000 --- a/src/test/java/com/fablen/elasticsearch/ElasticsearchTests.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fablen.elasticsearch; - - -import com.fablen.elasticsearch.entity.Book; -import com.fablen.elasticsearch.repository.BookRepository; -import org.junit.Before; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -/** - * @author zhangbaosheng - */ -@SpringBootTest -class ElasticsearchTests { - @Autowired - BookRepository bookRepository; - - @Test - void testDocument() { - - List books = new ArrayList<>(); - for (int i = 0; i < 100; i++) { - Book book = new Book(); - book.setId(i + 20000); - book.setName(UUID.randomUUID().toString()); - books.add(book); - } - bookRepository.saveAll(books); - - } - - // 定义一个全局的记录器,通过LoggerFactory获取 - private final static Logger log = LoggerFactory.getLogger(Test.class); - - @Before - public void setUp() { - } - - @Test - public void test() { - for (int i = 0; i < 10; i++) { - log.trace("trace 成功了"+i); - log.debug("debug 成功了"+i); - log.info("info 成功了"+i); - log.warn("warn 成功了"+i); - log.error("error 成功了"+i); - log.info("你好啊e"+i); - log.warn("This is a warn message!"+i); - log.error("This is error message!"+i); - } - } -}