Java日記-強力なElastisearch検索エンジンを使用
9865 ワード
Elastisearchは強力で、使いやすい検索エンジンでシステム上でElastisearchを実行するには以下のステップしかかかりません.
1.Elastisearchをダウンロード
2.解凍
3.運転
メモリオーバーフロー(OOM)、elastisearchが消費するメモリが非常に大きいため、メモリの小さいサーバで実行するにはjvmのメモリサイズを変更する必要があります.
22行と23行のスタックサイズを512 Mに変更
再起動してもkilledならもう少し小さくしてください
4.テストが成功したか
次のメッセージが表示されます.
起動に成功したことを示します
次にJavaのAPIでElasticsearchを操作します
まずelastisearchとlog 4 jをインポートしたパッケージ
POM
以下はよく使われるいくつかの操作です.
1.クライアントの作成
2.索引の作成
3.データの挿入
4.すべてのクエリー
5.キーワード検索
6.数値範囲フィルタ
7.ソート
8.ハイライトキー
9.idによる検索
10.削除
11.更新
よく使う操作はこれだけで、コードはhttps://github.com/jkgeekJack/ElasticsearchExample
Elasticsearch:権威ガイドもご覧になることをお勧めします.ここではもっと詳しく紹介します.
1.Elastisearchをダウンロード
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.zip
2.解凍
unzip elasticsearch-5.4.0.zip
3.運転
elasticsearch-5.4.0/bin/elasticsearch
メモリオーバーフロー(OOM)、elastisearchが消費するメモリが非常に大きいため、メモリの小さいサーバで実行するにはjvmのメモリサイズを変更する必要があります.
vi elasticsearch-5.4.0/config/jvm.options
22行と23行のスタックサイズを512 Mに変更
-Xms512M
-Xmx512M
再起動してもkilledならもう少し小さくしてください
4.テストが成功したか
curl 'http://localhost:9200/?pretty'
次のメッセージが表示されます.
{
"status": 200,
"name": "Shrunken Bones",
"version": {
"number": "1.4.0",
"lucene_version": "4.10"
},
"tagline": "You Know, for Search"
}
起動に成功したことを示します
次にJavaのAPIでElasticsearchを操作します
まずelastisearchとlog 4 jをインポートしたパッケージ
POM
4.0.0
com.jk
ElasticsearchExample
1.0-SNAPSHOT
org.elasticsearch.client
transport
5.4.0
org.apache.logging.log4j
log4j-api
2.7
以下はよく使われるいくつかの操作です.
1.クライアントの作成
Client client = null;
try {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (Exception e) {
e.printStackTrace();
}
2.索引の作成
/**
* ,
* @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.データの挿入
/**
*
* @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", " ");
s11.put("description", " java ");
s11.put("author", "Bruce Eckel");
s11.put("price", 108);
Map s12 = new LinkedHashMap();
s12.put("title", "Head First Java");
s12.put("origin", " ");
s12.put("description", "java ");
s12.put("author", "Kathy Sierra");
s12.put("price", 54);
Map s21 = new LinkedHashMap();
s21.put("title", "Design Pattern");
s21.put("origin", " ");
s21.put("description", " ");
s21.put("author", "Kathy Sierra");
s21.put("price", 89);
Map s22 = new LinkedHashMap();
s22.put("title", " ");
s22.put("origin", " ");
s22.put("description", " ");
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.すべてのクエリー
/**
*
*/
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.キーワード検索
/**
*
*
* @param client
*/
private static void searchKeyWord(Client client) {
SearchResponse response = client.prepareSearch(index)
//
.setQuery(QueryBuilders.matchQuery("_all", " "))
//
// .setQuery(QueryBuilders.matchQuery("_all", " ").minimumShouldMatch("100%"))
.execute().actionGet();
System.out.println("searchKeyWord : ");
System.out.println(response);
}
6.数値範囲フィルタ
/**
*
*
* @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())
//
.addSort(SortBuilders.fieldSort("price")
.order(SortOrder.DESC)).execute().actionGet();
System.out.println("searchOrdered : ");
System.out.println(response);
}
8.ハイライトキー
/**
*
* @param client
*/
private static void searchHightlight(Client client) {
//
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("title");
highlightBuilder.field("description");
SearchResponse response = client.prepareSearch(index)
// ,
// .setQuery(QueryBuilders.matchQuery("title", "java"))
// ,
.setQuery(QueryBuilders.multiMatchQuery(" ", "title", "description"))
.highlighter(highlightBuilder)
.execute()
.actionGet();
System.out.println("searchHightlight : ");
System.out.println(response);
}
9.idによる検索
/**
* 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
Elasticsearch:権威ガイドもご覧になることをお勧めします.ここではもっと詳しく紹介します.