前言
之前我們使用Solr全文搜索服務(wù)器來建立我們自己的搜索竹椒,本篇文章將介紹跟Solr類似的另一種搜索服務(wù)器——Elasticsearch滑废。就個人而言,Elasticsearch比Solr使用更方便竟痰,只要使用Http通過Json傳輸就可以去使用了盘榨。對于ElasticSearch服務(wù)器的部署,分布式朴译,集群這里就先不介紹了井佑。部署啟動后,訪問 http://127.0.0.1:9200/眠寿,顯示如下類似信息躬翁,說明服務(wù)器已經(jīng)起來了。
> {
"name" : "8n5uD4P",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "D-hKMOfgQQKjizry-i41qw",
"version" : {
"number" : "6.0.0",
"build_hash" : "8f0685b",
"build_date" : "2017-11-10T18:41:22.859Z",
"build_snapshot" : false,
"lucene_version" : "7.0.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
接下來就是通過客戶端去調(diào)用了盯拱,這里使用Java
依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>5.6.3</version>
</dependency>
Core(要存儲的對象)
package top.yuyufeng.learn.lucene.elasticsearch.core;
import java.util.Date;
/**
* @author yuyufeng
* @date 2017/12/6
*/
public class BlogCore {
private String blogId;
private String blogTitle;
private String blogContent;
private Date createTime;
private String keywords;
public String getBlogId() {
return blogId;
}
public void setBlogId(String blogId) {
this.blogId = blogId;
}
public String getBlogTitle() {
return blogTitle;
}
public void setBlogTitle(String blogTitle) {
this.blogTitle = blogTitle;
}
public String getBlogContent() {
return blogContent;
}
public void setBlogContent(String blogContent) {
this.blogContent = blogContent;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
@Override
public String toString() {
return "BlogCore{" +
"blogId='" + blogId + '\'' +
", blogTitle='" + blogTitle + '\'' +
", blogContent='" + blogContent + '\'' +
", createTime=" + createTime +
", keywords='" + keywords + '\'' +
'}';
}
}
增刪改查
package top.yuyufeng.learn.lucene.elasticsearch;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import top.yuyufeng.learn.lucene.elasticsearch.core.BlogCore;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
/**
* @author yuyufeng
* @date 2017/12/11
*/
public class ClientBasicTest {
private RestClient restClient;
private Response response;
private Header header;
@Before
public void testBefore() {
restClient = RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")).build();
header = new BasicHeader("Content-Type", "application/json");
}
/**
* 增加(更新)索引(帶Id)
*/
@Test
public void testIndexWithId() throws IOException {
BlogCore blog = new BlogCore();
blog.setBlogId("2");
blog.setBlogTitle("達(dá)摩院超越業(yè)界龍頭");
blog.setBlogContent("達(dá)摩院一定也必須要超越英特爾盒发,必須超越微軟例嘱,必須超越IBM,因?yàn)槲覀兩诙皇兰o(jì)宁舰,我們是有機(jī)會后發(fā)優(yōu)勢的拼卵。");
blog.setCreateTime(new Date());
String json = JSONObject.toJSONString(blog);
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("PUT", "/test/blog/"+blog.getBlogId(), Collections.emptyMap(), entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 增加索引(ID自動增長)
*/
@Test
public void testIndexWithAutoId() throws IOException {
BlogCore blog = new BlogCore();
blog.setBlogId("5");
blog.setBlogTitle("達(dá)摩院成立");
blog.setBlogContent("達(dá)摩院一定也必須要超越英特爾,必須超越微軟蛮艰,必須超越IBM腋腮,因?yàn)槲覀兩诙皇兰o(jì),我們是有機(jī)會后發(fā)優(yōu)勢的壤蚜。");
blog.setCreateTime(new Date());
String json = JSONObject.toJSONString(blog);
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("POST", "/test/blog/", Collections.emptyMap(), entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 刪除
*/
@Test
public void testDelete() throws IOException {
response = restClient.performRequest("DELETE", "/test/blog/2", Collections.emptyMap(), header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@After
public void testAfter() {
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
搜索
package top.yuyufeng.learn.lucene.elasticsearch;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import top.yuyufeng.learn.lucene.elasticsearch.core.BlogCore;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
/**
* @author yuyufeng
* @date 2017/12/11
*/
public class ClientSearchTest {
private RestClient restClient;
private Response response;
private Header header;
@Before
public void testBefore() {
restClient = RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")).build();
header = new BasicHeader("Content-Type", "application/json");
}
/**
* 輕量搜索1
*/
@Test
public void testSearch1() throws IOException {
response = restClient.performRequest("GET", "/test/blog/_search?pretty", header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 輕量搜索2
*/
@Test
public void testSearch2() throws IOException {
response = restClient.performRequest("GET", "/test/blog/_search?q=blogTitle:達(dá)摩&pretty", header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 使用查詢表達(dá)式搜索
*/
@Test
public void testSearchWithMatch() throws IOException {
String json = "{" +
" \"query\" : {" +
" \"match\" : {" +
" \"blogTitle\" : \"達(dá)摩阿里巴巴\"" +
" }" +
" }" +
"}";
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("GET", "/test/blog/_search?pretty", Collections.emptyMap(),entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 使用查詢表達(dá)式搜索
*/
@Test
public void testSearchWithMatchAndFilter() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"bool\": {\n" +
" \"must\": {\n" +
" \"match\" : {\n" +
" \"blogTitle\" : \"達(dá)摩\" \n" +
" }\n" +
" },\n" +
" \"filter\": {\n" +
" \"range\" : {\n" +
" \"blogId\" : { \"gt\" : 1 } \n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("GET", "/test/blog/_search?pretty", Collections.emptyMap(),entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 短語搜索 (單詞之間緊挨著)
* @throws IOException
*/
@Test
public void testSearchWithMatchPhrase() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"match_phrase\" : {\n" +
" \"blogContent\" : \"阿里巴巴達(dá)摩院\"\n" +
" }\n" +
" }\n" +
"}";
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("GET", "/test/blog/_search?pretty", Collections.emptyMap(),entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 高亮
* @throws IOException
*/
@Test
public void testSearchWithighlight() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"blogContent\" : \"阿里巴巴全球研究院\"\n" +
" }\n" +
" },\n" +
" \"highlight\": {\n" +
" \"fields\" : {\n" +
" \"blogContent\" : {}\n" +
" }\n" +
" }\n" +
"}";
HttpEntity entity = new StringEntity(json, "utf-8");
response = restClient.performRequest("GET", "/test/blog/_search?pretty", Collections.emptyMap(),entity, header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@After
public void testAfter() {
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}