快速掌握通過Spring Data框架操作Elasticsearch

一、通過原生的Java代碼操作ES

(1)運行環(huán)境:idea、maven3.5、spring

(2)pom.xml 文件中需要配置的工具包

? ? <maven.compiler.source>1.8

? ? <maven.compiler.target>1.8

? ? ? ? <!--測試類架包-->

? ? ? ? ? ? <groupId>junit

? ? ? ? ? ? <artifactId>junit

? ? ? ? ? ? <version>4.12

? ? ? ? ? ? <scope>test

? ? ? ? <!--ES 相關(guān)包-->

? ? ? ? ? ? <groupId>org.elasticsearch

? ? ? ? ? ? <artifactId>elasticsearch

? ? ? ? ? ? <version>5.6.1

? ? ? ? ? ? <groupId>org.elasticsearch.client

? ? ? ? ? ? <artifactId>transport

? ? ? ? ? ? <version>5.6.1

? ? ? ? <!--日志相關(guān)包-->

? ? ? ? ? ? <groupId>org.apache.logging.log4j

? ? ? ? ? ? <artifactId>log4j-to-slf4j

? ? ? ? ? ? <version>2.10.0

? ? ? ? ? ? <groupId>org.apache.logging.log4j

? ? ? ? ? ? <artifactId>log4j-core

? ? ? ? ? ? <version>2.9.0

? ? ? ? ? ? <groupId>org.slf4j

? ? ? ? ? ? <artifactId>slf4j-api

? ? ? ? ? ? <version>1.7.25

? ? ? ? ? ? <groupId>org.slf4j

? ? ? ? ? ? <artifactId>slf4j-simple

? ? ? ? ? ? <version>1.7.21

? ? ? ? ? ? <groupId>log4j

? ? ? ? ? ? <artifactId>log4j

? ? ? ? ? ? <version>1.2.12

? ? ? ? <!--多導(dǎo)入一個包,否則報日志錯誤-->

? ? ? ? ? ? <groupId>org.apache.logging.log4j

? ? ? ? ? ? <artifactId>log4j-api

? ? ? ? ? ? <version>2.9.1

? ? ? ? <!--json數(shù)據(jù)相關(guān)包-->

? ? ? ? ? ? <groupId>com.fasterxml.jackson.core

? ? ? ? ? ? <artifactId>jackson-core

? ? ? ? ? ? <version>2.8.1

? ? ? ? ? ? <groupId>com.fasterxml.jackson.core

? ? ? ? ? ? <artifactId>jackson-databind

? ? ? ? ? ? <version>2.8.1

? ? ? ? ? ? <groupId>com.fasterxml.jackson.core

? ? ? ? ? ? <artifactId>jackson-annotations

? ? ? ? ? ? <version>2.8.1

? ? ? ? <!--spring Data框架相關(guān)包-->

? ? ? ? ? ? <groupId>org.springframework.data

? ? ? ? ? ? <artifactId>spring-data-elasticsearch

? ? ? ? ? ? <version>3.0.5.RELEASE

? ? ? ? ? ? ? ? ? ? <groupId>org.elasticsearch.plugin

? ? ? ? ? ? ? ? ? ? <artifactId>transport-netty4-client

? ? ? ? ? ? <groupId>org.springframework

? ? ? ? ? ? <artifactId>spring-test

? ? ? ? ? ? <version>5.0.5.RELEASE

</project>


(3)在resources文件夾創(chuàng)建applicationContext.xml配置文件

??????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

??????xmlns:context="http://www.springframework.org/schema/context"

??????xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"

??????xsi:schemaLocation="http://www.springframework.org/schema/beans

???????? http://www.springframework.org/schema/beans/spring-beans.xsd

???????? http://www.springframework.org/schema/context

???????? http://www.springframework.org/schema/context/spring-context.xsd

???http://www.springframework.org/schema/data/elasticsearch

???http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">

???????????????????????????????????cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>

</beans>

(4)創(chuàng)建一個實體(構(gòu)建 :無參構(gòu)造、set、get方法)

@Document(indexName ="es_blog",type = "article")

public class Article {

???@Id

???@Field(type = FieldType.Long,store=true)

???private long id;

???@Field(type = FieldType.text,store=true,analyzer="ik_smart")

???private String title;

(5)創(chuàng)建一個接口

package com.xcy.repositories;

import com.xcy.pojo.Article;

importorg.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleRepository extendsElasticsearchRepository {

}

(6)測試

1.創(chuàng)建一個索引庫

2.向索引庫中插入數(shù)據(jù)

結(jié)果:

3.刪除庫中的數(shù)據(jù)

4.查詢


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容