ES基础
ElastisSearch --主要用途 做全文检索
最关键的是,ES无论是什么项目,中小型项目还是大型项目都能完美使用起来
为什么要使用ElasticSearch
Lucene 缺点:
使用起来很复杂
Lucene不支持集群
ES:特点:
(1) Es 全文检索框架 使用比lucene更简单
(2) ES支持集群 支持分布式
(3) 支持JSON的操作
(4) 一般大型全文检索都是用ES
(5) 通过Restfull风格来操作ES
ElasticSearch所要导入的包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
(1)使用之前需要打开服务器,+写下面的工具类
public static TransportClient getClient() throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); return client;
}
(2)然后对其简单的增删改查测试
增加
@Test
public void getCreated() throws UnknownHostException {
TransportClient client = getClient();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("crm", "user", "1");
Map<String,Object> mp = new HashMap();
mp.put("id",2);
mp.put("name","kg");
mp.put("age",18);
IndexResponse indexResponse = indexRequestBuilder.setSource(mp).get();
System.out.println(indexResponse