- Java后端-學習路線-筆記匯總表【黑馬程序員】
- ElasticSearch-學習筆記01【ElasticSearch基本介紹】【day01】
- ElasticSearch-學習筆記02【ElasticSearch索引庫維護】
- ElasticSearch-學習筆記03【ElasticSearch集群】
- ElasticSearch-學習筆記04【Java客戶端操作索引庫】【day02】
- ElasticSearch-學習筆記05【SpringDataElasticSearch】
目錄
01-ES課程介紹
02-使用Java客戶端創(chuàng)建索引庫
03-使用Java客戶端設置mapping步驟
04-使用java客戶端設置mapping映射
05-向索引庫中添加文檔
06-添加文檔的第二種方式
07-索引庫查詢_根據(jù)id查詢
?08-索引庫查詢_根據(jù)term查詢
09-索引庫查詢_queryString查詢
10-查詢分頁設置
01、插入數(shù)據(jù)
02、設置分頁
11-查詢結果高亮顯示
01、查看高亮結果
02、高亮顯示代碼實現(xiàn)
01-ES課程介紹
學習目標:
能夠使用java客戶端完成創(chuàng)建、刪除索引的操作
能夠使用java客戶端完成文檔的增刪改的操作
能夠使用java客戶端完成文檔的查詢操作
能夠完成文檔的分頁操作
能夠完成文檔的高亮查詢操作
能夠搭建Spring Data ElasticSearch的環(huán)境
能夠完成Spring Data ElasticSearch的基本增刪改查操作
能夠掌握基本條件查詢的方法命名規(guī)則
管理ES的客戶端工具:postman、head插件。
02-使用Java客戶端創(chuàng)建索引庫
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
?? ?步驟:
?? ?1)創(chuàng)建一個Java工程
?? ?2)添加jar包,添加maven的坐標
?? ?3)編寫測試方法實現(xiàn)創(chuàng)建索引庫
?? ??? ?1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要用于配置集群的名稱。
?? ??? ?2、創(chuàng)建一個客戶端Client對象
?? ??? ?3、使用client對象創(chuàng)建一個索引庫
?? ??? ?4、關閉client對象
9201、9202、9203:對外提供http服務的端口號;
9301、9302、9303:tcp形式連接ES服務器(InetSocketTransportAddress方法)。
package com.itheima.es;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;
public class ElasticSearchClientTest {
@Test
public void createIndex() throws Exception {
//1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要配置集群的名稱。
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個客戶端Client對象
TransportClient client = new PreBuiltTransportClient(settings);
//向client中添加三個ip地址的端口號,防止一個服務器不好使然后出現(xiàn)bug
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//3、使用client對象創(chuàng)建一個索引庫
client.admin().indices().prepareCreate("index_hello").get();//.get()執(zhí)行操作//index_hello索引名稱
//4、關閉client對象
client.close();
}
}
ElasticSearch搭建集群時的閃退問題
03-使用Java客戶端設置mapping步驟
?
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
?? ?步驟:
?? ?1)創(chuàng)建一個Java工程
?? ?2)添加jar包,添加maven的坐標
?? ?3)編寫測試方法實現(xiàn)創(chuàng)建索引庫
?? ??? ?1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要配置集群的名稱。
?? ??? ?2、創(chuàng)建一個客戶端Client對象
?? ??? ?3、使用client對象創(chuàng)建一個索引庫
?? ??? ?4、關閉client對象
2、使用Java客戶端設置Mappings
?? ?步驟:
?? ?1)創(chuàng)建一個Settings對象
?? ?2)創(chuàng)建一個Client對象
?? ?3)創(chuàng)建一個mapping信息,應該是一個json數(shù)據(jù),可以是字符串,也可以是XContextBuilder對象
?? ?4)使用client向es服務器發(fā)送mapping信息
?? ?5)關閉client對象
04-使用java客戶端設置mapping映射
package com.itheima.es;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;
public class ElasticSearchClientTest {
@Test
public void createIndex() throws Exception {
//1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要配置集群的名稱。
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個客戶端Client對象
TransportClient client = new PreBuiltTransportClient(settings);
//向client中添加三個ip地址的端口號,防止一個服務器不好使然后出現(xiàn)bug
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//3、使用client對象創(chuàng)建一個索引庫
client.admin().indices().prepareCreate("index_hello").get();//index_hello索引名稱、get()執(zhí)行操作
//4、關閉client對象
client.close();
}
@Test
public void setMappings() throws Exception {
//1、創(chuàng)建一個Settings對象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個Client對象,創(chuàng)建一個TransportClient對象
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//3、創(chuàng)建一個mapping信息,應該是一個json數(shù)據(jù),可以是字符串,也可以是XContextBuilder對象
/*
{
"article": {type名稱,表名:文章
"properties": {
"id": {//字段
"type": "long",
"store": true
// "index": "not_analyzed"
},
"title": {
"type": "text",
"store": true,
"index": true,
"analyzer": "ik_smart"http://ik_max_word、standard
},
"content": {
"type": "text",
"store": true,
"index": true,//analyzed
"analyzer": "ik_smart"http://ik_max_word、standard
}
}
}
}
*/
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")//type名稱
.startObject("properties")
.startObject("id")
.field("type", "long")
.field("store", true)
.endObject()
.startObject("title")
.field("type", "text")
.field("store", true)
.field("analyzer", "ik_smart")
.endObject()
.startObject("content")
.field("type", "text")
.field("store", true)
.field("analyzer", "ik_smart")
.endObject()
.endObject()
.endObject()
.endObject();
//4、使用client向es服務器發(fā)送mapping信息,使用client把mapping信息設置到索引庫中
client.admin().indices()
.preparePutMapping("index_hello")//設置要做映射的索引,設置索引庫名稱
.setType("article")//設置要做映射的type,設置type名稱
.setSource(builder)//mapping信息,可以是XContentBuilder對象也可以是json格式的字符串
.get();
//5、關閉client對象,關閉鏈接
client.close();
}
}
05-向索引庫中添加文檔
es集群可以包含多個索引(indices)(數(shù)據(jù)庫),每一個索引可以包含多個類型(types)(表),每一個類型包含多個文檔(documents)(行),然后每個文檔(數(shù)據(jù))包含多個字段(Fields)(列)。
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
?? ?步驟:
?? ?1)創(chuàng)建一個Java工程
?? ?2)添加jar包,添加maven的坐標
?? ?3)編寫測試方法實現(xiàn)創(chuàng)建索引庫
?? ??? ?1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要用于配置集群的名稱。
?? ??? ?2、創(chuàng)建一個客戶端Client對象
?? ??? ?3、使用client對象創(chuàng)建一個索引庫
?? ??? ?4、關閉client對象
2、使用Java客戶端設置Mappings
?? ?步驟:
?? ?1)創(chuàng)建一個Settings對象
?? ?2)創(chuàng)建一個Client對象
?? ?3)創(chuàng)建一個mapping信息,應該是一個json數(shù)據(jù),可以是字符串,也可以是XContextBuilder對象
?? ?4)使用client向es服務器發(fā)送mapping信息
?? ?5)關閉client對象
3、添加文檔(一行數(shù)據(jù))
?? ?步驟:
?? ?1)創(chuàng)建一個Settings對象
?? ?2)創(chuàng)建一個Client對象
?? ?3)創(chuàng)建一個文檔對象,創(chuàng)建一個json格式的字符串或者使用XContentBuilder
?? ?4)使用Client對象吧文檔添加到索引庫中
?? ?5)關閉client
每一步都要創(chuàng)建一個client對象,所以將client單獨提取出來。
package com.itheima.es;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
public class ElasticSearchClientTest {
private TransportClient client;
@Before
public void init() throws Exception {
//1、創(chuàng)建一個Settings對象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個Client對象,創(chuàng)建一個TransportClient對象
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
@Test
public void createIndex() throws Exception {
//1、創(chuàng)建一個Settings對象,相當于是一個配置信息,主要配置集群的名稱。
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個客戶端Client對象
TransportClient client = new PreBuiltTransportClient(settings);
//向client中添加三個ip地址的端口號,防止一個服務器不好使然后出現(xiàn)bug
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//3、使用client對象創(chuàng)建一個索引庫
client.admin().indices().prepareCreate("index_hello").get();//index_hello索引名稱、get()執(zhí)行操作
//4、關閉client對象
client.close();
}
@Test
public void setMappings() throws Exception {
//3、創(chuàng)建一個mapping信息,應該是一個json數(shù)據(jù),可以是字符串,也可以是XContextBuilder對象
/*
{
"article": {type名稱,表名:文章
"properties": {
"id": {//字段
"type": "long",
"store": true
// "index": "not_analyzed"
},
"title": {
"type": "text",
"store": true,
"index": true,
"analyzer": "ik_smart"http://ik_max_word、standard
},
"content": {
"type": "text",
"store": true,
"index": true,//analyzed
"analyzer": "ik_smart"http://ik_max_word、standard
}
}
}
}
*/
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")//type名稱
.startObject("properties")
.startObject("id")
.field("type", "long")
.field("store", true)
.endObject()
.startObject("title")
.field("type", "text")
.field("store", true)
.field("analyzer", "ik_smart")
.endObject()
.startObject("content")
.field("type", "text")
.field("store", true)
.field("analyzer", "ik_smart")
.endObject()
.endObject()
.endObject()
.endObject();
//4、使用client向es服務器發(fā)送mapping信息,使用client把mapping信息設置到索引庫中
client.admin().indices()
.preparePutMapping("index_hello")//設置要做映射的索引,設置索引庫名稱
.setType("article")//設置要做映射的type,設置type名稱
.setSource(builder)//mapping信息,可以是XContentBuilder對象也可以是json格式的字符串
.get();
//5、關閉client對象,關閉鏈接
client.close();
}
@Test
public void testAddDocument() throws Exception {
//1)創(chuàng)建一個Settings對象
//2)創(chuàng)建一個Client對象
//public void init() throws Exception {...}
//3)創(chuàng)建一個文檔對象,創(chuàng)建一個json格式的字符串或者使用XContentBuilder
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id", 2l)//long型數(shù)據(jù) 2l
.field("title", "Cause I got a crush on you who you~")
.field("content", "你是我的我是你的誰~")
.endObject();
//4)使用Client對象把文檔對象添加到索引庫中
client.prepareIndex()
.setIndex("index_hello")//設置索引名稱
.setType("article")//設置typr
.setId("2")//設置文檔的id,如果不設置id的話es會自動生成一個id
.setSource(builder)//設置文檔信息,builder對象或json串
.get();//執(zhí)行操作
//5)關閉client客戶端
client.close();
}
}
06-添加文檔的第二種方式
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
2、使用Java客戶端設置Mappings
3、添加文檔(一行數(shù)據(jù))4、添加文檔第二種方式
?? ?創(chuàng)建一個pojo類
?? ?使用工具類把pojo轉換成json字符串
?? ?把文檔寫入索引庫
@Test
public void testAddDocument2() throws Exception {
//創(chuàng)建一個Article對象
Article article = new Article();
//設置對象的屬性
article.setId(3l);
article.setTitle("再多一眼看一眼就會爆炸~");
article.setContent("再近一點靠近點快被融化~");
//把article對象轉換成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//使用client對象把文檔寫入索引庫
client.prepareIndex("index_hello", "article", "3")
.setSource(jsonDocument, XContentType.JSON)
.get();
//關閉客戶端
client.close();
}
07-索引庫查詢_根據(jù)id查詢
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
2、使用Java客戶端設置Mappings
3、添加文檔(一行數(shù)據(jù))
4、添加文檔的第二種方式二、使用ES客戶端實現(xiàn)搜索
1、根據(jù)id搜索
?? ?QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
2、根據(jù)Term查詢(關鍵詞)
?? ?QueryBuilder queryBuilder = QueryBuilders.termQuery("title", "北方");
3、QueryString查詢方式(帶分析的查詢)
?? ?QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("速度與激情").defaultField("title");
?? ?查詢步驟:
?? ?1)創(chuàng)建一個Client對象
?? ?2)創(chuàng)建一個查詢對象,可以使用QueryBuilders工具類創(chuàng)建QueryBuilder對象
?? ?3)使用client執(zhí)行查詢
?? ?4)得到查詢的結果
?? ?5)取查詢結果的總記錄數(shù)
?? ?6)取查詢結果列表
?? ?7)關閉client
package com.itheima.es;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
public class SearchIndex {
private TransportClient client;
@Before
public void init() throws Exception {
//1、創(chuàng)建一個Settings對象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個Client對象,創(chuàng)建一個TransportClient對象
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
@Test
public void testSearchById() throws Exception {
//1)創(chuàng)建一個client對象
//public void init() throws Exception {}
//2)創(chuàng)建一個查詢對象,可以使用QueryBuilders工具類創(chuàng)建QueryBuilder對象
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
//3)使用client執(zhí)行查詢
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
.get();
//4)得到查詢的結果
SearchHits searchHits = searchResponse.getHits();
//5)取查詢結果的總記錄數(shù)
System.out.println("查詢結果總記錄數(shù):" + searchHits.getTotalHits());
//6)取查詢結果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文檔對象,以json格式輸出
System.out.println(searchHit.getSourceAsString());
//取文檔的屬性
System.out.println("---------------文檔的屬性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//7)關閉client
client.close();
}
}
?08-索引庫查詢_根據(jù)term查詢
package com.itheima.es;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
public class SearchIndex {
private TransportClient client;
@Before
public void init() throws Exception {
//1、創(chuàng)建一個Settings對象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個Client對象,創(chuàng)建一個TransportClient對象
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
private void search(QueryBuilder queryBuilder) throws Exception {
//3)使用client執(zhí)行查詢
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
.get();
//4)得到查詢的結果
SearchHits searchHits = searchResponse.getHits();
//5)取查詢結果的總記錄數(shù)
System.out.println("查詢結果總記錄數(shù):" + searchHits.getTotalHits());
//6)取查詢結果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文檔對象,以json格式輸出
System.out.println(searchHit.getSourceAsString());
//取文檔的屬性
System.out.println("---------------文檔的屬性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//7)關閉client
client.close();
}
@Test
public void testSearchById() throws Exception {
//1)創(chuàng)建一個client對象
//public void init() throws Exception {}
//2)創(chuàng)建一個查詢對象,可以使用QueryBuilders工具類創(chuàng)建QueryBuilder對象
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
search(queryBuilder);
}
@Test
public void testQueryByTerm() throws Exception {
//創(chuàng)建一個QueryBuilder對象
//參數(shù)1:要搜索的字段
//參數(shù)2:要搜索的關鍵詞
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("title", "爆炸");
//執(zhí)行查詢
search(queryBuilder);
}
}
09-索引庫查詢_queryString查詢
10-查詢分頁設置
01、插入數(shù)據(jù)
@Test
public void testAddDocument3() throws Exception {//批量添加數(shù)據(jù)
for (int i = 4; i < 100; i++) {
//創(chuàng)建一個Article對象
Article article = new Article();
//設置對象的屬性
article.setId(i);
article.setTitle("再多一眼看一眼就會爆炸~" + i);
article.setContent("再近一點靠近點快被融化~" + i);
//把article對象轉換成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//使用client對象把文檔寫入索引庫
client.prepareIndex("index_hello", "article", i + "")
.setSource(jsonDocument, XContentType.JSON)
.get();
}
//關閉客戶端
client.close();
}
02、設置分頁
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
2、使用Java客戶端設置Mappings
3、添加文檔(一行數(shù)據(jù))
4、添加文檔的第二種方式二、使用ES客戶端實現(xiàn)搜索
1、根據(jù)id搜索
2、根據(jù)Term查詢(關鍵詞)
3、QueryString查詢方式(帶分析的查詢)
4、分頁的處理
?? ?在client對象執(zhí)行查詢之前,設置分頁信息。
?? ?然后再執(zhí)行查詢
?? ? //執(zhí)行查詢
? ? ? ? SearchResponse searchResponse = client.prepareSearch("index_hello")
? ? ? ? ? ? ? ? .setTypes("article")
? ? ? ? ? ? ? ? .setQuery(queryBuilder)
? ? ? ? ? ? ? ? //設置分頁信息
? ? ? ? ? ? ? ? .setFrom(0)
? ? ? ? ? ? ? ? //每頁顯示的行數(shù)
? ? ? ? ? ? ? ? .setSize(5)
? ? ? ? ? ? ? ? .get();
?? ?分頁需要設置兩個值,一個from、size
?? ?from:起始的行號,從0開始。
?? ?size:每頁顯示的記錄數(shù)
11-查詢結果高亮顯示
一、使用Java客戶端管理ES
1、創(chuàng)建索引庫
2、使用Java客戶端設置Mappings
3、添加文檔(一行數(shù)據(jù))
4、添加文檔的第二種方式二、使用ES客戶端實現(xiàn)搜索
1、根據(jù)id搜索
2、根據(jù)Term查詢(關鍵詞)
3、QueryString查詢方式(帶分析的查詢)
4、分頁的處理
5、查詢結果高亮顯示
(1)高亮的配置
?? ??? ?1)設置高亮顯示的字段
?? ??? ?2)設置高亮顯示的前綴
?? ??? ?3)設置高亮顯示的后綴
(2)在client對象執(zhí)行查詢之前,設置高亮顯示的信息
(3)遍歷結果列表時可以從結果中取高亮結果
01、查看高亮結果
給關鍵詞的前后加上HTML標簽作為前后綴。
02、高亮顯示代碼實現(xiàn)
package com.itheima.es;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
public class SearchIndex {
private TransportClient client;
@Before
public void init() throws Exception {
//1、創(chuàng)建一個Settings對象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
//2、創(chuàng)建一個Client對象,創(chuàng)建一個TransportClient對象
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
private void search(QueryBuilder queryBuilder) throws Exception {
//3)使用client執(zhí)行查詢
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
.get();
//4)得到查詢的結果
SearchHits searchHits = searchResponse.getHits();
//5)取查詢結果的總記錄數(shù)
System.out.println("查詢結果總記錄數(shù):" + searchHits.getTotalHits());
//6)取查詢結果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文檔對象,以json格式輸出
System.out.println(searchHit.getSourceAsString());
//取文檔的屬性
System.out.println("---------------文檔的屬性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//7)關閉client
client.close();
}
@Test
public void testSearchById() throws Exception {
//1)創(chuàng)建一個client對象
//public void init() throws Exception {}
//2)創(chuàng)建一個查詢對象,可以使用QueryBuilders工具類創(chuàng)建QueryBuilder對象
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
search(queryBuilder);
/*
//3)使用client執(zhí)行查詢
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
.get();
//4)得到查詢的結果
SearchHits searchHits = searchResponse.getHits();
//5)取查詢結果的總記錄數(shù)
System.out.println("查詢結果總記錄數(shù):" + searchHits.getTotalHits());
//6)取查詢結果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文檔對象,以json格式輸出
System.out.println(searchHit.getSourceAsString());
//取文檔的屬性
System.out.println("---------------文檔的屬性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//7)關閉client
client.close();*/
}
@Test
public void testQueryByTerm() throws Exception {
//創(chuàng)建一個QueryBuilder對象
//參數(shù)1:要搜索的字段
//參數(shù)2:要搜索的關鍵詞
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("title", "爆炸");
//執(zhí)行查詢
search(queryBuilder);
}
@Test
public void testQueryStringQuery() throws Exception {
//創(chuàng)建一個QueryBuilder對象
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("爆炸").defaultField("title");
//執(zhí)行查詢
search(queryBuilder, "title");
}
private void search(QueryBuilder queryBuilder, String highLightField) throws Exception {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field(highLightField);//高亮顯示的字段
highlightBuilder.preTags("<em>");//前綴
highlightBuilder.postTags("</em>");//后綴
//3)使用client執(zhí)行查詢
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
//設置分頁信息
.setFrom(0)//起始行號從0開始
.setSize(5)//每一頁顯示的行數(shù)
//設置高亮信息
.highlighter(highlightBuilder)
.get();
//4)得到查詢的結果
SearchHits searchHits = searchResponse.getHits();
//5)取查詢結果的總記錄數(shù)
System.out.println("查詢結果總記錄數(shù):" + searchHits.getTotalHits());
//6)取查詢結果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
//打印文檔對象,以json格式輸出
System.out.println(searchHit.getSourceAsString());
//取文檔的屬性
System.out.println("---------------文檔的屬性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
System.out.println("****************************高亮結果");
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
System.out.println(highlightFields);
//取title高亮顯示的結果
HighlightField field = highlightFields.get(highLightField);//取key
Text[] fragments = field.getFragments();
if (fragments != null) {
String title = fragments[0].toString();
System.out.println(title);
}
}
//7)關閉client
client.close();
}
}
高亮顯示:
文章來源:http://www.zghlxwxcb.cn/news/detail-408717.html
xxx.xxx.var——快速生成變量。文章來源地址http://www.zghlxwxcb.cn/news/detail-408717.html
到了這里,關于ElasticSearch-學習筆記04【Java客戶端操作索引庫】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!