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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值