启东市住房和城乡建设局网站,抚州北京网站建设,哪里app开发公司好,奖励网站源码首先在官网下载elasticsearch8.9版本#xff0c;以及8.9版本的kibana。
解压#xff0c;点击es8.9bin目录下的elasticsearch.bat文件启动es
如图所示即为成功。 启动之后打开idea#xff0c;添加依赖 dependencygroupIdcom.fasterxml.jackson.core/g…首先在官网下载elasticsearch8.9版本以及8.9版本的kibana。
解压点击es8.9bin目录下的elasticsearch.bat文件启动es
如图所示即为成功。 启动之后打开idea添加依赖 dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.13.2/version/dependencydependencygroupIdorg.glassfish/groupIdartifactIdjakarta.json/artifactIdversion2.0.1/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdco.elastic.clients/groupIdartifactIdelasticsearch-java/artifactIdversion8.9.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.7.10/version/dependency
之后配置配置文件
Configuration
public class ElasticSearchConfig {Beanpublic ElasticsearchClient elasticsearchClient(){RestClient client RestClient.builder(new HttpHost(localhost, 9200,http)).build();ElasticsearchTransport transport new RestClientTransport(client,new JacksonJsonpMapper());return new ElasticsearchClient(transport);}
}
这时候就已经可以使用了基本使用操作如下代码块 SpringBootTest
class SpringDataJpaApplicationTests {Autowiredprivate ElasticsearchClient client;/*创建索引*/Testvoid test01() throws Exception {//写法比RestHighLevelClient更加简洁CreateIndexResponse indexResponse client.indices().create(c - c.index(user));}//查询数据Testpublic void queryTest() throws IOException {GetIndexResponse getIndexResponse client.indices().get(i - i.index(user));System.out.println(getIndexResponse);}//判断索引是否存在Testpublic void existsTest() throws IOException {BooleanResponse booleanResponse client.indices().exists(e - e.index(user));System.out.println(booleanResponse.value());}//删除索引Testpublic void deleteTest() throws IOException {DeleteIndexResponse deleteIndexResponse client.indices().delete(d - d.index(user));System.out.println(deleteIndexResponse.acknowledged());}//插入documentTestpublic void addDocumentTest() throws IOException {User user new User(1, 张三,123123123);IndexResponse indexResponse client.index(i - i.index(user)//设置id.id(1)//传入user对象.document(user));}//更新documentTestpublic void updateDocumentTest() throws IOException {UpdateResponseUser updateResponse client.update(u - u.index(user).id(1).doc(new User(1,user2,123132131)), User.class);}//查询documentTestpublic void queryDocumentTest() throws IOException {GetResponseUser response client.get(g - g.index(user).id(1), User.class);System.out.println(response);System.out.println(response.source());}//删除documentTestpublic void deleteDocumentTest() throws IOException {DeleteResponse response client.delete(d - d.index(user).id(1));System.out.println(response);}//批量插入documentTestpublic void bulkTest() throws IOException {ListUser usersnew CopyOnWriteArrayList();users.add(new User(1,张1,1233));users.add(new User(2,张2,1234));users.add(new User(3,张3,1235));users.add(new User(4,张4,1236));users.add(new User(5,张5,1237));List BulkOperation bulkOperationCopyOnWriteArrayList new CopyOnWriteArrayList();//遍历插入bulk中users.stream().forEach(u-{bulkOperationCopyOnWriteArrayList.add(BulkOperation.of(o -o.index(i-i.document(u))));});System.out.println(bulkOperationCopyOnWriteArrayList);BulkResponse responseclient.bulk(b-b.index(user).operations(bulkOperationCopyOnWriteArrayList));System.out.println(response);}//查询
/* Testpublic void searchTest() throws IOException {SearchResponseUser search client.search(s - s.index(user)//查询name字段包含hello的document(不使用分词器精确查找).query(q - q.term(t - t.field(name).value(v - v.stringValue(hello))))//分页查询从第0页开始查询3个document.from(0).size(3)//按age降序排序.sort(f-f.field(o-o.field(age).order(SortOrder.Desc))),User.class);for (HitUser hit : search.hits().hits()) {System.out.println(hit.source());}}*/}