Java 日记——使用强大的 Elastisearch 搜索引擎
Elastisearch 是一个很强大,易用的搜索引擎在系统上运行 Elastisearch 只需以下几步
1.下载 Elastisearchwget 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
这时有可能会直接被 Killed 掉,因为内存溢出(OOM),elastisearch 占用的内存非常大,所以在内存比较小的服务器上运行要先修改 jvm 的内存大小
vi elasticsearch-5.4.0/config/jvm.options
将 22 和 23 行的栈堆大小改为 512M
-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 和 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>
</project>
以下是常用的几种操作
1.创建 clientClient 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);}
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);
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,小于 100setQuery(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();}
评论