検索学習--Elasticsearch全文検索サーバーの基本使用
11589 ワード
前言
以前はSolr全文検索サーバーを使って自分達の検索を確立しました。この記事ではSolrと似たようなもう一つの検索サーバーElasticsearchを紹介します。個人的には、ElasticsearchはSolrよりも使いやすく、Httpを使ってJsonを通じて伝送すれば使えます。ElasticSearchサーバの配置については、分散し、ここではクラスタを紹介しない。配置開始後、アクセスhttp://127.0.0.1:9200/下記のような情報が表示されます。サーバーがすでに起きています。
“name”:“8 n 5 uD 4 P”、“cluster name”:“elasticsearch”、“clusteruuuuid”:“D-hKMOfgQQKjizry-41qw”、“version”:{nuuuuuuudber”:“6.0.0”、“builduhashsh”:“b b”:“85文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献:“文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献1:“_version:「7.0.1」、「minimum uwiregbility uversion」:「5.6.0」、「minimum uuindexclicatibility uversion」:「5..0」、「tagline」:「You Know、for Search」)は次にクライアントを通じて呼び出されます。ここでJavaを使います。
依存https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
以前はSolr全文検索サーバーを使って自分達の検索を確立しました。この記事ではSolrと似たようなもう一つの検索サーバーElasticsearchを紹介します。個人的には、ElasticsearchはSolrよりも使いやすく、Httpを使ってJsonを通じて伝送すれば使えます。ElasticSearchサーバの配置については、分散し、ここではクラスタを紹介しない。配置開始後、アクセスhttp://127.0.0.1:9200/下記のような情報が表示されます。サーバーがすでに起きています。
“name”:“8 n 5 uD 4 P”、“cluster name”:“elasticsearch”、“clusteruuuuid”:“D-hKMOfgQQKjizry-41qw”、“version”:{nuuuuuuudber”:“6.0.0”、“builduhashsh”:“b b”:“85文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献:“文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献文献1:“_version:「7.0.1」、「minimum uwiregbility uversion」:「5.6.0」、「minimum uuindexclicatibility uversion」:「5..0」、「tagline」:「You Know、for Search」)は次にクライアントを通じて呼び出されます。ここでJavaを使います。
依存
com.alibaba
fastjson
1.2.38
junit
junit
4.12
test
org.elasticsearch.client
elasticsearch-rest-client
5.6.3
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(" ");
blog.setBlogContent(" , , IBM, , 。");
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(" ");
blog.setBlogContent(" , , IBM, , 。");
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: &pretty", header);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
*
*/
@Test
public void testSearchWithMatch() throws IOException {
String json = "{" +
" \"query\" : {" +
" \"match\" : {" +
" \"blogTitle\" : \" \"" +
" }" +
" }" +
"}";
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()));
}
/**
*
*/
@Test
public void testSearchWithMatchAndFilter() throws IOException {
String json = "{
" +
" \"query\" : {
" +
" \"bool\": {
" +
" \"must\": {
" +
" \"match\" : {
" +
" \"blogTitle\" : \" \"
" +
" }
" +
" },
" +
" \"filter\": {
" +
" \"range\" : {
" +
" \"blogId\" : { \"gt\" : 1 }
" +
" }
" +
" }
" +
" }
" +
" }
" +
"}";
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 = "{
" +
" \"query\" : {
" +
" \"match_phrase\" : {
" +
" \"blogContent\" : \" \"
" +
" }
" +
" }
" +
"}";
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 = "{
" +
" \"query\" : {
" +
" \"match\" : {
" +
" \"blogContent\" : \" \"
" +
" }
" +
" },
" +
" \"highlight\": {
" +
" \"fields\" : {
" +
" \"blogContent\" : {}
" +
" }
" +
" }
" +
"}";
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();
}
}
}
参考文献