TestElasticsearchBoost

package com.itheima.elasticsearch.test;

import java.awt.MultipleGradientPaint;
import java.io.IOException;
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.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.elasticsearch.domain.Article;

public class TestElasticsearchBoost {

private Client client;


/**获取Transport Client
 * @throws Exception */
@Before
public void getClient() throws Exception{
	// ElasticSearch 服务默认端口 9300
	client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(
			InetAddress.getByName("127.0.0.1"),9300));
}


/**
 * 10.1.索引相关操作 (IK分词)
 * * 创建索引
 * * 删除索引
 */
@Test
public void testCreateIndex() throws Exception{
	// 创建索引
	client.admin().indices().prepareCreate("blog1").get();
	//关闭
	client.close();
}

/**
 * 创建映射
 */
@Test
public void testCreateIndexMapping() throws Exception{
	//构建json的数据格式,创建映射
	XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
			.startObject()
				.startObject("article")
					.startObject("properties")
						.startObject("id")
							.field("type","integer").field("store", "yes")
						.endObject()
						.startObject("title")
							.field("type","string").field("store", "yes").field("analyzer","ik")
						.endObject()
						.startObject("content")
							.field("type","string").field("store", "yes").field("analyzer","ik")

// .field(“type”,“string”).field(“store”, “yes”).field(“analyzer”,“ik”).field(“boost”,10)
.endObject()
.startObject(“comment”)
.field(“type”,“string”).field(“store”, “yes”).field(“analyzer”,“ik”)
.endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest request = Requests.putMappingRequest(“blog1”)
.type(“article”)
.source(mappingBuilder);
client.admin().indices().putMapping(request).get();
//关闭
client.close();
}

/**使用Jackson(处理对象和json数据之间的转换),创建文档
 * @throws Exception */
@Test
public void createDocumentJacksonMapping() throws Exception{
	
	Article article = new Article();

// article.setId(1);
// article.setTitle(“搜索引擎服务器”); // 有搜索
// article.setContent(“基于restful的数据风格”); // 无搜索
// article.setComment(“我们学习Elasticsearch搜索引擎服务器”);// 有搜索

	article.setId(2);
	article.setTitle("什么是Elasticsearch"); // 无搜索
	article.setContent("Elasticsearch搜索引擎服务器"); // 有搜索
	article.setComment("Elasticsearch封装了lucene");// 无搜索
	
	ObjectMapper objectMapper = new ObjectMapper();
	String source = objectMapper.writeValueAsString(article);
	System.out.println("source:"+source);
	
	IndexResponse indexResponse = client.prepareIndex("blog1", "article", article.getId().toString()).setSource(source).get();
	// 获取响应的信息
	System.out.println("索引名称:"+indexResponse.getIndex());
	System.out.println("文档类型:"+indexResponse.getType());
	System.out.println("ID:"+indexResponse.getId());
	System.out.println("版本:"+indexResponse.getVersion());
	System.out.println("是否创建成功:"+indexResponse.isCreated());
	client.close();
}



/**
 * SearchResponse获取,支持各种查询:IK分词器后的查询
 */
@Test
public void testQueryString() throws Exception{
	SearchResponse searchResponse = client.prepareSearch("blog1")
			.setTypes("article")
			// 在3个字段上完成搜索
			.setQuery(QueryBuilders.queryStringQuery("搜索").field("content^10").field("title^5").field("comment"))
			.get();
	SearchHits hits = searchResponse.getHits();
	
	this.searchValue(hits);
	//关闭
	client.close();
}

/**
 * SearchResponse获取,支持各种查询:IK分词器后的查询
 */
@Test
public void testBoolQuery() throws Exception{
	SearchResponse searchResponse = client.prepareSearch("blog1")
			.setTypes("article")
			// 在3个字段上完成搜索
			.setQuery(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("title", "搜索").boost(5f))
					.should(QueryBuilders.termQuery("content", "搜索").boost(10f))
					.should(QueryBuilders.termQuery("comment", "搜索")))
			.get();
	SearchHits hits = searchResponse.getHits();
	
	this.searchValue(hits);
	//关闭
	client.close();
}




// 显示查询的结果数据
private void searchValue(SearchHits hits) {
	System.out.println("查询的结果数量有"+hits.getTotalHits()+"条");
	System.out.println("结果中最多的分数:"+hits.getMaxScore());
	// 遍历每条数据
	Iterator<SearchHit> iterator = hits.iterator();
	while(iterator.hasNext()){
		SearchHit searchHit = iterator.next();
		// System.out.println("所有的数据JSON的数据格式:"+searchHit.getSourceAsString());
		System.out.println("当前文章的得分:"+searchHit.getScore());
		// 获取每个字段的数据
		System.out.println("id:"+searchHit.getSource().get("id"));
		System.out.println("title:"+searchHit.getSource().get("title"));
		System.out.println("content:"+searchHit.getSource().get("content"));
		System.out.println("comment:"+searchHit.getSource().get("comment"));
		System.out.println("**********************************************");
		for(Iterator<SearchHitField> ite = searchHit.iterator();ite.hasNext();){
			SearchHitField next = ite.next();
			System.out.println(next.getValues());
		}
	}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值