進行基本的搜索:
包括: 查詢所有、解析字符串查詢、通配符查詢、詞條查詢
package com.es.querydemo;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.es.bean.Product;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 查詢
*
* @author Beck
* @date 2018年2月6日
*/
public class TestESQuery {
private static final String HOST = "127.0.0.1";
private static final int PORT = 9300;
private static final String INDEX = "eshop";
private static final String TYPE = "product";
private static final ObjectMapper MAPPER = new ObjectMapper();
private TransportClient client = null;
// 詞條查詢
@Test
public void termQueryTermQuery(){
SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
// 搜索會忽略大小寫,使用小寫來搜索
.setQuery(QueryBuilders.termQuery("title", "手機"))
.get();
// 查詢的總數(命中數)
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
System.out.println("總記錄數: " + totalHits);
// 遍歷查詢的結果
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
// System.out.println(next.getSourceAsString());
String id = next.getId();
Map<String, Object> source = next.getSource();
Integer productId = (Integer) source.get("id");
String productTitle = (String) source.get("title");
System.out.println("Document ID: " + id);
System.out.println("商品的id: " + productId);
System.out.println("商品的title: " + productTitle);
}
}
// 通配符查詢
@Test
public void searchByWildcardQuery(){
SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
// 搜索會忽略大小寫,使用小寫來搜索
.setQuery(QueryBuilders.wildcardQuery("title", "apple*"))
.get();
// 查詢的總數(命中數)
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
System.out.println("總記錄數: " + totalHits);
// 遍歷查詢的結果
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
// System.out.println(next.getSourceAsString());
String id = next.getId();
Map<String, Object> source = next.getSource();
Integer productId = (Integer) source.get("id");
String productTitle = (String) source.get("title");
System.out.println("Document ID: " + id);
System.out.println("商品的id: " + productId);
System.out.println("商品的title: " + productTitle);
}
}
// 解析字符串查詢,會先分詞,然後查詢
@Test
public void searchByString(){
SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
.setQuery(QueryBuilders.queryStringQuery("華爲手機"))
.get();
// 查詢的總數(命中數)
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
System.out.println("總記錄數: " + totalHits);
// 遍歷查詢的結果
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
// System.out.println(next.getSourceAsString());
String id = next.getId();
Map<String, Object> source = next.getSource();
Integer productId = (Integer) source.get("id");
String productTitle = (String) source.get("title");
System.out.println("Document ID: " + id);
System.out.println("商品的id: " + productId);
System.out.println("商品的title: " + productTitle);
}
}
// 查詢所有
@Test
public void searchAll(){
SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
.setQuery(QueryBuilders.matchAllQuery())
.get();
// 查詢的總數(命中數)
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
System.out.println("總記錄數: " + totalHits);
// 遍歷查詢的結果
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()){
SearchHit next = iterator.next();
// System.out.println(next.getSourceAsString());
String id = next.getId();
Map<String, Object> source = next.getSource();
Integer productId = (Integer) source.get("id");
String productTitle = (String) source.get("title");
System.out.println("Document ID: " + id);
System.out.println("商品的id: " + productId);
System.out.println("商品的title: " + productTitle);
}
}
// 獲取客戶端
@Before
public void getClient() throws Exception{
client = TransportClient.builder()
.build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
}
// 關閉客戶端
@After
public void closeClient(){
if (this.client != null){
this.client.close();
}
}
}