1,下载Elasticsearch
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.tar.gz
2,上传并解压
tar -zxvf elasticsearch-5.5.2.tar.gz -C es/
提前说一下:我jdk版本:
$ java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
3,启动
就这么简单?是的!启动
./elasticsearch-5.5.2/bin/elasticsearch
会报:istoo low, increase toatleast [65536]
需要修改:sudo vi /etc/security/limits.conf
加上
* soft nofile 65536
* hard nofile 65536
修改:sudo vi /etc/sysctl.conf
加上
vm.max_map_count = 2621441
好了!再次启动,启动起来了
注意看ip是127.0.0.1这个只能本机访问,我这里是虚拟机,那么我电脑怎么访问呢?那怎么修改呢?
先杀掉:ps -ef|grep elastic
kill -9 2441
修改:vi elasticsearch.yml
network.host: 0.0.0.0
重启!./elasticsearch-5.5.2/bin/elasticsearch
安装成功!单节点的
这里还有一个很重要的东西就是中文分词器,采用的是ik,就是一个插件
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.2/elasticsearch-analysis-ik-5.5.2.zip
这个一定要和es的版本保持一致,以免造成版本不兼容,出现很多无法预料的问题
====================================
接下来就是java中怎么应用
在pom.xml中加入:
这里要格外关注的是client的版本和服务器的版本保持一致,并且jackson版本要和es版本匹配,怎么才叫匹配呢?
这里有答案:http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch/5.5.2
从图上可以看出jackson的版本2.8.6
一个测试类
package com.sun.elasticsearch;
import com.google.gson.JsonObject;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author songlj
* @date 2018/4/11
*/
public class ElasticsearchTest {
private Loggerlogger = LoggerFactory.getLogger(ElasticsearchTest.class);
public final static StringHOST ="192.168.146.128";
public final static int PORT =9300;//http请求的端口是9200,客户端是9300
private TransportClientclient =null;
/**
* 获取客户端连接信息
*/
@SuppressWarnings({"resource","unchecked" })
@Before
public void getConnect()throws UnknownHostException {
client =new PreBuiltTransportClient(Settings.EMPTY).addTransportAddresses(
new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));
logger.info("连接成功");
}
/**
* 关闭连接
*/
@After
public void closeConnect() {
if(null !=client) {
logger.info("执行关闭连接操作...");
client.close();
}
}
/**
* 创建索引库
*/
@Test
public void addIndex()throws IOException {
IndexResponse response =client.prepareIndex("tx_application","user","1").setSource(XContentFactory.jsonBuilder()
.startObject().field("userName","Rock")
.field("msg","I am Rock_JieGe")
.endObject()).get();
logger.info("索引名称:" + response.getIndex() +"\n类型:" + response.getType()
+"\n文档ID:" + response.getId() +"\n当前实例状态:" + response.status());
}
/**
* 从索引库获取数据
*/
@Test
public void getData() {
GetResponse getResponse =client.prepareGet("tx_application","user","1").get();
logger.info("索引库的数据:" + getResponse.getSourceAsString());
}
/**
* 更新索引库数据
*/
@Test
public void updateData() {
JsonObject jsonObject =new JsonObject();
jsonObject.addProperty("userName","Doug Lee");
jsonObject.addProperty("sendDate","20180411");
jsonObject.addProperty("msg","I am Doug Lee");
UpdateResponse updateResponse =client.prepareUpdate("tx_application","user","1")
.setDoc(jsonObject.toString(), XContentType.JSON).get();
logger.info("updateResponse索引名称:" + updateResponse.getIndex() +"\n updateResponse类型:" + updateResponse.getType()
+"\n updateResponse文档ID:" + updateResponse.getId() +"\n当前实例updateResponse状态:" + updateResponse.status());
}
/**
* 根据索引名称,类别,文档ID 删除索引库的数据
*/
@Test
public void deleteData() {
DeleteResponse deleteResponse =client.prepareDelete("tx_application","user","1").get();
logger.info("deleteResponse索引名称:" + deleteResponse.getIndex() +"\n deleteResponse类型:" + deleteResponse.getType()
+"\n deleteResponse文档ID:" + deleteResponse.getId() +"\n当前实例deleteResponse状态:" + deleteResponse.status());
}
/**
* 添加索引json格式的
*/
@Test
public void addIndexJson() {
String jsonStr ="{" +
"\"userName\":\"MiLi\"," +
"\"sendDate\":\"2017-04-18\"," +
"\"msg\":\"MiLi say Hello word\"" +
"}";
IndexResponse response =client.prepareIndex("county","xian").setSource(jsonStr, XContentType.JSON).get();
logger.info("json索引名称:" + response.getIndex() +"\njson类型:" + response.getType()
+"\njson文档ID:" + response.getId() +"\n当前实例json状态:" + response.status());
}
}