Java使用強(qiáng)大的Elastisearch搜索引擎實(shí)例代碼
Elastisearch是一個(gè)很強(qiáng)大,易用的搜索引擎
在系統(tǒng)上運(yùn)行Elastisearch只需以下幾步
1.下載Elastisearch
2.解壓
unzip elasticsearch-5.4.0.zip
3.運(yùn)行
elasticsearch-5.4.0/bin/elasticsearch
這時(shí)有可能會(huì)直接被Killed掉,因?yàn)閮?nèi)存溢出(OOM),elastisearch占用的內(nèi)存非常大,所以在內(nèi)存比較小的服務(wù)器上運(yùn)行要先修改jvm的內(nèi)存大小
vi elasticsearch-5.4.0/config/jvm.options
將22和23行的棧堆大小改為512M
-Xms512M -Xmx512M
如果重新啟動(dòng)后還是killed就再改小一點(diǎn)
4.測(cè)試是否成功
curl 'http://localhost:9200/?pretty'
你能看到以下返回信息:
{ "status": 200, "name": "Shrunken Bones", "version": { "number": "1.4.0", "lucene_version": "4.10" }, "tagline": "You Know, for Search" }
則表明啟動(dòng)成功
接下來(lái)我們用Java的API來(lái)操作Elasticsearch
首先是導(dǎo)入elastisearch和log4j的包
POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jk</groupId> <artifactId>ElasticsearchExample</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <!--用哪個(gè)版本就填什么--> <version>5.4.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> </dependencies> </project>
以下是常用的幾種操作
1.創(chuàng)建client
Client client = null; try { client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); } catch (Exception e) { e.printStackTrace(); }
2.創(chuàng)建索引
/** * 創(chuàng)建索引,有則先刪除 * @param client */ private static void recreateIndex(Client client) { if (client.admin().indices().prepareExists(index).execute().actionGet() .isExists()) { DeleteIndexResponse deleteIndexResponse = client.admin().indices() .delete(new DeleteIndexRequest(index)).actionGet(); System.out.println("delete index :"); System.out.println(deleteIndexResponse); } CreateIndexResponse createIndexResponse = client.admin().indices() .prepareCreate(index).execute().actionGet(); System.out.println("create index :"); System.out.println(createIndexResponse); }
3.插入數(shù)據(jù)
/** * 插入數(shù)據(jù) * @param client */ @SuppressWarnings({"rawtypes", "unchecked"}) private static void doIndex(final Client client) { Map s11 = new LinkedHashMap(); s11.put("title", "Think in java"); s11.put("origin", "美國(guó)"); s11.put("description", "初級(jí)java開(kāi)發(fā)人員必讀的書(shū)"); s11.put("author", "Bruce Eckel"); s11.put("price", 108); Map s12 = new LinkedHashMap(); s12.put("title", "Head First Java"); s12.put("origin", "英國(guó)"); s12.put("description", "java入門(mén)教材"); s12.put("author", "Kathy Sierra"); s12.put("price", 54); Map s21 = new LinkedHashMap(); s21.put("title", "Design Pattern"); s21.put("origin", "法國(guó)"); s21.put("description", "程序員不得不讀的設(shè)計(jì)模式"); s21.put("author", "Kathy Sierra"); s21.put("price", 89); Map s22 = new LinkedHashMap(); s22.put("title", "黑客與畫(huà)家"); s22.put("origin", "法國(guó)"); s22.put("description", "讀完之后腦洞大開(kāi)"); s22.put("author", "Paul Graham"); s22.put("price", 35); BulkResponse bulkResponse = client .prepareBulk() .add(client.prepareIndex(index, type).setId("11").setSource(s11).setOpType(IndexRequest.OpType.INDEX).request()) .add(client.prepareIndex(index, type).setId("12").setSource(s12).setOpType(IndexRequest.OpType.INDEX).request()) .add(client.prepareIndex(index, type).setId("21").setSource(s21).setOpType(IndexRequest.OpType.INDEX).request()) .add(client.prepareIndex(index, type).setId("22").setSource(s22).setOpType(IndexRequest.OpType.INDEX).request()) .execute().actionGet(); if (bulkResponse.hasFailures()) { System.err.println("index docs ERROR:" + bulkResponse.buildFailureMessage()); } else { System.out.println("index docs SUCCESS:"); System.out.println(bulkResponse); } }
4.查詢(xún)所有
/** * 查詢(xún)所有 */ private static void searchAll(Client client) { SearchResponse response = client.prepareSearch(index) .setQuery(QueryBuilders.matchAllQuery()) .setExplain(true).execute().actionGet(); System.out.println("searchAll : "); for (SearchHit searchHit : response.getHits()) { System.out.println("********"); System.out.println(searchHit.getSource()); } }
5.關(guān)鍵詞查詢(xún)
/** * 關(guān)鍵詞查詢(xún) * * @param client */ private static void searchKeyWord(Client client) { SearchResponse response = client.prepareSearch(index) //查詢(xún)所有字段匹配關(guān)鍵字 .setQuery(QueryBuilders.matchQuery("_all", "法國(guó)")) //設(shè)置最小匹配程度 // .setQuery(QueryBuilders.matchQuery("_all", "法國(guó)").minimumShouldMatch("100%")) .execute().actionGet(); System.out.println("searchKeyWord : "); System.out.println(response); }
6.數(shù)值范圍過(guò)濾
/** * 數(shù)值范圍過(guò)濾 * * @param client */ private static void searchRange(Client client) { SearchResponse response = client.prepareSearch(index). //大于80,小于100 setQuery(QueryBuilders.rangeQuery("price").gt(80).lt(100)) .execute() .actionGet(); System.out.println("searchRange : "); System.out.println(response); }
7.排序
/** * 排序 * * @param client */ private static void searchOrdered(Client client) { SearchResponse response = client.prepareSearch(index) .setQuery(QueryBuilders.matchAllQuery()) //根據(jù)價(jià)格降序排序 .addSort(SortBuilders.fieldSort("price") .order(SortOrder.DESC)).execute().actionGet(); System.out.println("searchOrdered : "); System.out.println(response); }
8.高亮關(guān)鍵字
/** * 高亮關(guān)鍵字 * @param client */ private static void searchHightlight(Client client) { //高亮多個(gè)字段 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("title"); highlightBuilder.field("description"); SearchResponse response = client.prepareSearch(index) //單條件匹配,高亮?xí)r只能高亮該字段 // .setQuery(QueryBuilders.matchQuery("title", "java")) //多條件匹配,高亮?xí)r只能高亮多個(gè)字段 .setQuery(QueryBuilders.multiMatchQuery("開(kāi)發(fā)人員必讀", "title", "description")) .highlighter(highlightBuilder) .execute() .actionGet(); System.out.println("searchHightlight : "); System.out.println(response); }
9.根據(jù)id查找
/** * 根據(jù)id查找 * @param client */ private static void findById(final Client client) { String id="12"; GetResponse response = client.prepareGet(index, type, id).get(); System.out.println("findById"); System.out.println(response); }
10.刪除
/** * 刪除 * @param client */ private static void deleteById(Client client) { String id="12"; DeleteResponse response = client.prepareDelete(index, type, id).get(); }
11.更新
/** * 更新 * @param client */ private static void updateById(Client client) { try { String id="11"; client.prepareUpdate(index, type, id) .setDoc(jsonBuilder() .startObject() .field("title", "白鹿原") .endObject()) .get(); } catch (IOException e) { e.printStackTrace(); } }
常用的操作就這些,代碼上傳到https://github.com/jkgeekJack/ElasticsearchExample
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java中EnumMap代替序數(shù)索引代碼詳解
- Java應(yīng)用開(kāi)源框架實(shí)現(xiàn)簡(jiǎn)易web搜索引擎
- Java使用分治算法實(shí)現(xiàn)排序數(shù)索引功能示例【二分搜索】
- mongodb索引知識(shí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- JAVA實(shí)現(xiàn)空間索引編碼——GeoHash的示例
- java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎
- java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法
- java多線(xiàn)程處理執(zhí)行solr創(chuàng)建索引示例
- Java數(shù)組索引異常產(chǎn)生及解決方案
相關(guān)文章
Java工具類(lèi)DateUtils實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Java工具類(lèi)DateUtils實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12基于springboot的RestTemplate、okhttp和HttpClient對(duì)比分析
這篇文章主要介紹了基于springboot的RestTemplate、okhttp和HttpClient對(duì)比分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Java多線(xiàn)程文件分片下載實(shí)現(xiàn)的示例代碼
這篇文章主要介紹了Java多線(xiàn)程文件分片下載實(shí)現(xiàn)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03java實(shí)現(xiàn)最短路徑算法之Dijkstra算法
這篇文章主要介紹了java實(shí)現(xiàn)最短路徑算法之Dijkstra算法, Dijkstra算法是最短路徑算法中為人熟知的一種,是單起點(diǎn)全路徑算法,有興趣的可以了解一下2017-10-10IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project報(bào)錯(cuò)問(wèn)題的解決)
這篇文章主要介紹了IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project問(wèn)題的解決),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06java二叉樹(shù)的幾種遍歷遞歸與非遞歸實(shí)現(xiàn)代碼
這篇文章主要介紹了java二叉樹(shù)的幾種遍歷遞歸與非遞歸實(shí)現(xiàn)代碼,需要的朋友可以參考下2020-12-12