1,新建Client
@Before
public void init() throws UnknownHostException {
client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
start = System.currentTimeMillis();
}
@After
public void end(){
System.out.println("cost time:"+(System.currentTimeMillis() - start)+"ms");
client.close();
}
2,创建索引
@Test
public void testCreate(){
User user = mockUserData(0);
IndexResponse response = client.prepareIndex(index,type,user.getId())
.setSource(JSONObject.toJSONString(user))
.get();
System.out.println("data is created:"+response.isCreated());
System.out.println(JSONObject.toJSONString(response));
}
3,更新索引
@Test
public void testUpdate() throws ExecutionException, InterruptedException {
User user = new User();
user.setId("1");
user.setEmail("540*****5@qq.com");
UpdateResponse response = client.prepareUpdate(index,type,user.getId()).setDoc(JSONObject.toJSONString(user)).get();
System.out.println(JSONObject.toJSONString(response));
}
4,删除索引
@Test
public void testDelete(){
DeleteResponse response = client.prepareDelete(index,type,"1").get();
System.out.println(JSONObject.toJSONString(response));
}
5,查询
@Test
public void testQueryById(){
QueryBuilder idsQuery = QueryBuilders.idsQuery().addIds("14","19");
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setQuery(idsQuery)
.setExplain(true)
.execute()
.actionGet();
System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
}
@Test
public void testTermQuery(){
QueryBuilder termQuery = QueryBuilders.termQuery("age","44");
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setQuery(termQuery)
.setFrom(0)
.setSize(50)
.setExplain(true)
.execute()
.actionGet();
System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
}
@Test
public void testRangeQuery(){
QueryBuilder rangeQuery = QueryBuilders.rangeQuery("age").from(10).to(50);
SearchResponse response = client.prepareSearch(index)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setTypes(type)
.setQuery(rangeQuery)
.setFrom(0)
.setSize(50)
.execute()
.actionGet();
System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
}
// 在某几个列中搜索关键字
@Test
public void testMultiMatchQuery(){
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("故事","articleTitle","articleContent");
SearchResponse response = client.prepareSearch("test2")
.setTypes("article")
.setHighlighterQuery(queryBuilder)
.setHighlighterRequireFieldMatch(true)
.addHighlightedField("articleContent")
.addHighlightedField("articleTitle")
.setHighlighterPreTags("<span>")
.setHighlighterPostTags("</span>")
.setFrom(0)
.setSize(50)
.execute()
.actionGet();
SearchHits hits = response.getHits();
System.out.println("总量"+hits.getTotalHits());
SearchHit[] hitArray = hits.hits();
for (SearchHit hit : hitArray) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//打印高亮显示内容
for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
}
参考:elasticsearch官网Java API
网友评论