Elasticsearch Java API 的使用(16)—实现match_all查询、match查询 和 multimath查询

match_all
查询所有

public class EsMatchAll{
    public void updateIndex(TransportClient client){
	   //查询所有(match_all)
       QueryBuilder qb = QueryBuilders.matchAllQuery();
       
       SearchResponse sr = client.prepareSearch("lib3")
							     .setQuery(qb)
								 .get();
	   
	   SearchHits hits = sr.getHits();
	   
	   for(Searchhit hit:hits){
		   //整个文档输出
		   System.out.println(hit.getSourceAsString);
		   //文档中每个字段数据
		   Map<String, Object> map = hit.getSourceAsMap();
		   for(String key:map.keySet()){
			   System.out.println(key+"="+map.get(key))
		   }
	   }				
    }
}

match query
指定单个字段条件匹配查询

public class EsMatchAll{
    public void updateIndex(TransportClient client){
	   //单个字段条件匹配查询(match)
       QueryBuilder qb = QueryBuilders.matchQuery("interests","changge");
       
       SearchResponse sr = client.prepareSearch("lib3")
							     .setQuery(qb)
							     .setSize(3)
							     .get();
	   
	   SearchHits hits = sr.getHits();
	   
	   for(Searchhit hit:hits){
		   //整个文档输出
		   System.out.println(hit.getSourceAsString);
		   //文档中每个字段数据
		   Map<String, Object> map = hit.getSourceAsMap();
		   for(String key:map.keySet()){
			   System.out.println(key+"="+map.get(key))
		   }
	   }				
    }
}

multiMatchQuery
指定多个字段条件匹配查询

public class EsMatchAll{
    public void updateIndex(TransportClient client){
	   //多个字段条件匹配查询(multiMatchQuery)
       QueryBuilder qb = QueryBuilders.multiMatchQuery("changge","address","interests");
       
       SearchResponse sr = client.prepareSearch("lib3")
							     .setQuery(qb)
							     .setSize(3)
							     .get();
	   
	   SearchHits hits = sr.getHits();
	   
	   for(Searchhit hit:hits){
		   //整个文档输出
		   System.out.println(hit.getSourceAsString);
		   //文档中每个字段数据
		   Map<String, Object> map = hit.getSourceAsMap();
		   for(String key:map.keySet()){
			   System.out.println(key+"="+map.get(key))
		   }
	   }				
    }
}

你可能感兴趣的:(Elasticsearch Java API 的使用(16)—实现match_all查询、match查询 和 multimath查询)