Spring Boot + Elasticsearch 實(shí)現(xiàn)索引的日常維護(hù)

全文檢索的應(yīng)用越來越廣泛郭宝,幾乎成了互聯(lián)網(wǎng)應(yīng)用的標(biāo)配辞槐,商品搜索、日志分析粘室、歷史數(shù)據(jù)歸檔等等榄檬,各種場(chǎng)景都會(huì)涉及到大批量的數(shù)據(jù),在全文檢索方面衔统,方案無外乎Lucene鹿榜、Solr、Elasticsearch三種應(yīng)用的較為廣泛锦爵。es舱殿、solr的底層都依托于Lucene,但es比solr學(xué)習(xí)成本更低险掀,由于其提供的RESTful API簡(jiǎn)單快捷沪袭,對(duì)互聯(lián)網(wǎng)應(yīng)用開發(fā)而言更是如虎添翼。

下面結(jié)合以實(shí)際案例樟氢,通過Java API的形式操作es數(shù)據(jù)集冈绊。

框架選型基礎(chǔ)是Spring Boot + Spring-data-elasticsearch + elasticsearch。

使用ElasticsearchRepository的形式來連接埠啃、維護(hù)ES數(shù)據(jù)集死宣,ElasticsearchRepository中提供了簡(jiǎn)單的操作索引數(shù)據(jù)的方法集合,繼承自ElasticsearchCrudRepository霸妹,涵蓋了CRUD十电、排序、分頁等常見的基本操作功能。

@NoRepositoryBean ?

public interface ElasticsearchRepository extends ElasticsearchCrudRepository { ?

? ? S index(S var1); ?

? ?Iterable search(QueryBuilder var1); ?

? ?Page search(QueryBuilder var1, Pageable var2); ?

? ?Page search(SearchQuery var1); ?

? ?Page searchSimilar(T var1, String[] var2, Pageable var3); ?

? ?void refresh(); ?

? ?Class getEntityClass(); ?

} ?

編寫自己的Resository操作類

public interface ArticleSearchRepository extends ElasticsearchRepository{

? ?List findByAbstractsAndContent(String abstracts, String content);

}

其中Article為是與elasticsearch連接的實(shí)體類鹃骂,類似于PO的概念台盯,其中指定的索引名稱、類型名稱畏线、及分片静盅、副本數(shù)量等要素。

@Data

@Document(indexName = "article_index", type = "article", shards = 5, replicas = 1, indexStoreType = "fs", refreshInterval = "-1")

public class Article implements Serializable {

? ?/**

? ? * serialVersionUID:

? ? *

? ? * @since JDK 1.6

? ? */

? ?private static final long serialVersionUID = 1L;

? ?@Id

? ?private Long id;

? ?/** 標(biāo)題 */

? ?private String title;

? ?/** 摘要 */

? ?private String abstracts;

? ?/** 內(nèi)容 */

? ?private String content;

? ?/** 發(fā)表時(shí)間 */

? ?@Field(format = DateFormat.date_time, index = FieldIndex.no, store = true, type = FieldType.Object)

? ?private Date postTime;

? ?/** 點(diǎn)擊率 */

? ?private Long clickCount;

}

我們需要定義域的實(shí)體和一個(gè)Spring data的基本的CRUD支持庫類寝殴。用id注釋定義標(biāo)識(shí)符字段蒿叠,如果你沒有指定ID字段,Elasticsearch不能索引你的文件蚣常。同時(shí)需要指定索引名稱類型市咽,@Document注解也有助于我們?cè)O(shè)置分片和副本數(shù)量。

接口類

public interface ArticleService {

? ?/**

? ? * saveArticle: 寫入

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:06

? ? * @param article

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?long saveArticle(Article article);

? ?/**

? ? * deleteArticle: 刪除抵蚊,并未真正刪除施绎,只是查詢不到

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:08

? ? * @param id

? ? * @since JDK 1.6

? ? */

? ?void deleteArticle(long id);

? ?/**

? ? * findArticle:

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:10

? ? * @param id

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?Article findArticle(long id);

? ?/**

? ? * findArticlePageable:

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:13

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?List findArticlePageable();

? ?/**

? ? * findArticleAll:

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:15

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?List findArticleAll();

? ?/**

? ? * findArticleSort:

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:18

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?List findArticleSort();

? ?/**

? ? * search:

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:22

? ? * @param content

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?List search(String content);

? ?/**

? ? * update: es沒有修改操作,結(jié)合save操作完成

? ? *

? ? * @author guooo Date:2017年9月27日下午3:20:25

? ? * @param id

? ? * @return

? ? * @since JDK 1.6

? ? */

? ?long update(long id);

}

接口實(shí)現(xiàn)

@Service

public class ArticleServiceImpl implements ArticleService {

? ?final int page = 0;

? ?final int size = 10;

? ?/* 搜索模式 */

? ?String SCORE_MODE_SUM = "sum"; // 權(quán)重分求和模式

? ?Float MIN_SCORE = 10.0F; // 由于無相關(guān)性的分值默認(rèn)為 1 贞绳,設(shè)置權(quán)重分最小值為 10

? ?Pageable pageable = new PageRequest(page, size);

? ?@Autowired

? ?ArticleSearchRepository repository;

? ?@Override

? ?public long saveArticle(Article article) {

? ? ? ?Article result = repository.save(article);

? ? ? ?return result.getId();

? ?}

? ?@Override

? ?public void deleteArticle(long id) {

? ? ? ?repository.delete(id);

? ?}

? ?@Override

? ?public Article findArticle(long id) {

? ? ? ?return repository.findOne(id);

? ?}

? ?@Override

? ?public List findArticlePageable() {

? ? ? ?return repository.findAll(pageable).getContent();

? ?}

? ?@Override

? ?public List findArticleAll() {

? ? ? ?Iterable iterables = repository.findAll();

? ? ? ?List articles = new ArrayList<>();

? ? ? ?for (Article article : iterables) {

? ? ? ? ? ?articles.add(article);

? ? ? ?}

? ? ? ?return articles;

? ?}

? ?@Override

? ?public List findArticleSort() {

? ? ? ?List orders = new ArrayList<>();

? ? ? ?Order order = new Order(Direction.ASC, "clickCount");

? ? ? ?orders.add(order);

? ? ? ?Sort sort = new Sort(orders);

? ? ? ?Iterable iterables = repository.findAll(sort);

? ? ? ?List articles = new ArrayList<>();

? ? ? ?for (Article article : iterables) {

? ? ? ? ? ?articles.add(article);

? ? ? ?}

? ? ? ?return articles;

? ?}

? ?@Override

? ?public List search(String content) {

? ? ? ?return repository.findByAbstractsAndContent(content, content);

? ?}

? ?@Override

? ?public long update(long id) {

? ? ? ?Article article = repository.findOne(id);

? ? ? ?article.setTitle("test");

? ? ? ?Article retun = repository.save(article);

? ? ? ?System.out.println(retun.getId()+"更新的數(shù)據(jù)");

? ? ? ?return retun.getId();

? ?}

}

是不是與JPA谷醉、hibernate操作數(shù)據(jù)集的手法很類似?

controller方法類:

@RestController

@RequestMapping(value = "/article")

public class APIArticleController {

? ?@Autowired

? ?ArticleService articleService;

? ?@RequestMapping(value = "save", method = RequestMethod.POST)

? ?public long save() {

? ? ? ?for (int i = 10000; i < 12000; i++) {

? ? ? ? ? ?Article article = new Article();

? ? ? ? ? ?article.setClickCount(Long.valueOf(i + RandomUtils.nextInt(23, i)));

? ? ? ? ? ?article.setAbstracts("我的一個(gè)測(cè)試" + i);

? ? ? ? ? ?article.setContent(i + "這是第一個(gè)測(cè)試的內(nèi)容@spring-data-elasticsearch");

? ? ? ? ? ?article.setPostTime(new Date());

? ? ? ? ? ?article.setId(Long.valueOf(RandomUtils.nextLong(i, i)));

? ? ? ? ? ?long _id = articleService.saveArticle(article);

? ? ? ? ? ?System.out.println(_id);

? ? ? ?}

? ? ? ?return 23;

? ?}

? ?@RequestMapping(value = "delete", method = RequestMethod.POST)

? ?public void deleteArticle(long id) {

? ? ? ?articleService.deleteArticle(id);

? ?}

? ?@RequestMapping(value = "findOne", method = RequestMethod.POST)

? ?public Article findArticle(long id) {

? ? ? ?return articleService.findArticle(id);

? ?}

? ?@RequestMapping(value = "findArticlePageable", method = RequestMethod.POST)

? ?public List findArticlePageable() {

? ? ? ?return articleService.findArticlePageable();

? ?}

? ?@RequestMapping(value = "findArticleAll", method = RequestMethod.POST)

? ?public List findArticleAll() {

? ? ? ?return articleService.findArticleAll();

? ?}

? ?@RequestMapping(value = "findArticleSort", method = RequestMethod.POST)

? ?public List findArticleSort() {

? ? ? ?return articleService.findArticleSort();

? ?}

? ?@RequestMapping(value = "search", method = RequestMethod.POST)

? ?public List search(String content) {

? ? ? ?return articleService.search(content);

? ?}

? ?@RequestMapping(value = "update", method = RequestMethod.POST)

? ?public long update(long id) {

? ? ? ?return articleService.update(id);

? ?}

}

Spring Boot的啟動(dòng)類及配置項(xiàng)冈闭,這里略過俱尼,項(xiàng)目啟動(dòng)后,可能過controller暴露出來的方法進(jìn)行Article數(shù)據(jù)索引的CRUD操作萎攒。

擴(kuò)展閱讀:

Spring Boot + Elasticsearch 實(shí)現(xiàn)索引的日常維護(hù)

Spring Boot + Elasticsearch 實(shí)現(xiàn)索引批量寫入

Nginx+Lua+MySQL/Redis實(shí)現(xiàn)高性能動(dòng)態(tài)網(wǎng)頁展現(xiàn)

Nginx+Lua+Redis實(shí)現(xiàn)高性能緩存數(shù)據(jù)讀取

基于SpringCloud的Microservices架構(gòu)實(shí)戰(zhàn)案例-序篇

介紹幾款常用的在線API管理工具

野蠻生長(zhǎng)的前端遇八,從雜牌軍到正規(guī)軍

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌太颤,老刑警劉巖裆悄,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門雳刺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人裸违,你說我怎么就攤上這事掖桦。” “怎么了供汛?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵枪汪,是天一觀的道長(zhǎng)涌穆。 經(jīng)常有香客問我,道長(zhǎng)雀久,這世上最難降的妖魔是什么宿稀? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮赖捌,結(jié)果婚禮上祝沸,老公的妹妹穿的比我還像新娘。我一直安慰自己越庇,他們只是感情好罩锐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著卤唉,像睡著了一般涩惑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上桑驱,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天境氢,我揣著相機(jī)與錄音,去河邊找鬼碰纬。 笑死,一個(gè)胖子當(dāng)著我的面吹牛问芬,可吹牛的內(nèi)容都是我干的悦析。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼此衅,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼强戴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起挡鞍,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤骑歹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后墨微,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體道媚,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年翘县,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了最域。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锈麸,死狀恐怖镀脂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情忘伞,我是刑警寧澤薄翅,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布沙兰,位于F島的核電站,受9級(jí)特大地震影響翘魄,放射性物質(zhì)發(fā)生泄漏鼎天。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一熟丸、第九天 我趴在偏房一處隱蔽的房頂上張望训措。 院中可真熱鬧,春花似錦光羞、人聲如沸绩鸣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呀闻。三九已至,卻和暖如春潜慎,著一層夾襖步出監(jiān)牢的瞬間捡多,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國打工铐炫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留垒手,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓倒信,卻偏偏與公主長(zhǎng)得像科贬,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鳖悠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理榜掌,服務(wù)發(fā)現(xiàn),斷路器乘综,智...
    卡卡羅2017閱讀 134,657評(píng)論 18 139
  • 在使用Eleasticsearch進(jìn)行索引維護(hù)的過程中憎账,如果你的應(yīng)用場(chǎng)景需要頻繁的大批量的索引寫入,再使用上篇中提...
    MavenTalk閱讀 12,387評(píng)論 0 0
  • 一步一步的搭建JAVA WEB項(xiàng)目卡辰,采用Maven構(gòu)建胞皱,基于MYBatis+Spring+Spring MVC+B...
    葉子的翅膀閱讀 12,665評(píng)論 5 25
  • 1:初心|遇見你 2:筆記|你知道寫作嗎(一) 3:筆記|你知道寫作嗎(二) 4:《如何有效閱讀一本書》——你知道...
    魚十七123閱讀 322評(píng)論 3 1
  • 最后一次給你講我聽過的故事 她挽著他的手臂,他給她披上大衣 臉上的皺紋笑開了花九妈,白發(fā)里夾雜著雪花 兩個(gè)佝僂的影子在...
    夜枝閱讀 153評(píng)論 0 4