ElasticSearch(7)--使用Java客户端进行基本搜索

进行基本的搜索:

包括: 查询所有、解析字符串查询、通配符查询、词条查询

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();
		}
	}
}


猜你喜欢

转载自blog.csdn.net/beck2017/article/details/79278386