1. 程式人生 > >Elasticsearch Java API 的使用(17)—實現term查詢和terms查詢

Elasticsearch Java API 的使用(17)—實現term查詢和terms查詢

term查詢
trem查詢只可指定查詢一個欄位對應單個詞條

public class EsTermQuery{
    public void updateIndex(TransportClient client){
       //查詢一個欄位對應單個詞條(term)
       QueryBuilder qb = QueryBuilders.termQuery("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)) } } } }

terms查詢
trems查詢可指定查詢一個欄位對應多個詞條

public class EsTermsQuery{
    public void updateIndex(TransportClient client){
       //查詢一個欄位對應多個詞條(terms)
       QueryBuilder qb = QueryBuilders.termsQuery("interests", "changge", "lvyou");

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