ElasticSearch (服務默認端口 9300 Web 管理平臺端口 9200)

@Test
public void elastic() throws Exception{
//創建搜索服務器對象
Client client = TransportClient
.builder()
.build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300 ));

XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id","1")
.field("title","elasticsearch的入門案列")
.field("context","ElasticSearch是一個基於Lucene的搜索服務器。"
+ "它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。")
.endObject();
//創建文檔對象
client.prepareIndex("blog1", "article", "1").setSource(builder).get();

//關閉連接
client.close();

}



@Test 

public void sechar() throws Exception{
//創建連接
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.01"), 9300));
//搜索數據
SearchResponse searchResponse = client.prepareSearch("blog1")
.setTypes("article")
.setQuery(QueryBuilders.matchAllQuery()).get(); //查詢所有
SearchHits hits = searchResponse.getHits();
System.out.println("查詢數據的條數" + hits.getTotalHits());
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println(next.getSourceAsString());
System.out.println("title:" + next.getSource().get("title"));
}
// 關閉client
client.close();
}




//默認分詞機制

@Test 
public void sechar1() throws Exception{
//創建連接
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.01"), 9300));
//搜索數據
SearchResponse searchResponse = client.prepareSearch("blog1")
.setTypes("article")
.setQuery(QueryBuilders.queryStringQuery("程全")).get(); //默認分詞機制   全 ,程 單個詞
searchSelect(client, searchResponse);
}


//模糊查詢
@Test 
public void sechar2() throws Exception{
//創建連接
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.01"), 9300));
//搜索數據
SearchResponse searchResponse = client.prepareSearch("blog1")
.setTypes("article")
.setQuery(QueryBuilders.wildcardQuery("context", "*程全*")).get();  //查詢不到 說明詞條中沒有 程全
searchSelect(client, searchResponse);
}

//termQuery 詞條查詢
@Test 
public void sechar3() throws Exception{
//創建連接
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.01"), 9300));
//搜索數據
SearchResponse searchResponse = client.prepareSearch("blog1")
.setTypes("article")
.setQuery(QueryBuilders.termQuery("context", "全")).get();  //查詢不到 說明詞條中沒有 程全
searchSelect(client, searchResponse);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章