【Elasticsearh】-【match】查询Java代码示例【版本号:6.5.4】

import java.io.IOException;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.study.es.ESClient;

public class MatchQuery {
        ObjectMapper mapper = new ObjectMapper();
        RestHighLevelClient client = ESClient.getClient();
        String index = "sms-logs-index";
        String type = "sms-logs-type";
        
        @Test
        public void multiMatch() throws IOException {
            System.out.println("----------------------------------------------------multiMatch-------------------------------------------------------------------------");
            SearchRequest sr = new SearchRequest(index).types(type);
            
            SearchSourceBuilder builder = new SearchSourceBuilder();
            builder.query(QueryBuilders.multiMatchQuery("上海", "smsContent","province"));
            sr.source(builder);
            
            SearchResponse resp = client.search(sr, RequestOptions.DEFAULT);
            for (SearchHit hit : resp.getHits().getHits()) {
                Map<String,Object> result = hit.getSourceAsMap();
                System.out.println("match----length---" + resp.getHits().getHits().length );
                System.out.println("match--- result----" + result );
            }
            System.out.println("------------------------------------------------------------------------------------------------------------------------------");
        }
        
        @Test
        public void matchBool() throws IOException {
            SearchRequest sr = new SearchRequest(index).types(type);
            
            SearchSourceBuilder builder = new SearchSourceBuilder();
            builder.query(QueryBuilders.matchQuery("smsContent", "滴滴 银行").operator(Operator.OR));
            sr.source(builder);
            
            SearchResponse resp = client.search(sr, RequestOptions.DEFAULT);
            for (SearchHit hit : resp.getHits().getHits()) {
                Map<String,Object> result = hit.getSourceAsMap();
                System.out.println("match----length---" + resp.getHits().getHits().length );
                System.out.println("match--- result----" + result );
            }
            System.out.println("------------------------------------------------------------------------------------------------------------------------------");
        }
        
        @Test
        public void match() throws IOException {
            SearchRequest sr = new SearchRequest(index).types(type);
            
            SearchSourceBuilder builder = new SearchSourceBuilder();
            builder.query(QueryBuilders.matchQuery("smsContent", "滴滴生活"));
            sr.source(builder);
            
            SearchResponse resp = client.search(sr, RequestOptions.DEFAULT);
            for (SearchHit hit : resp.getHits().getHits()) {
                Map<String,Object> result = hit.getSourceAsMap();
                System.out.println("match-------" + resp.getHits().getHits().length );
                System.out.println("match--- result----" + result );
            }
            System.out.println("------------------------------------------------------------------------------------------------------------------------------");
        }
        
        @Test
        public void matchAll() throws IOException {
            SearchRequest sr = new SearchRequest(index).types(type);
            
            SearchSourceBuilder builder = new SearchSourceBuilder();
            builder.query(QueryBuilders.matchAllQuery());
            builder.size(20);
            sr.source(builder);
            
            SearchResponse resp = client.search(sr, RequestOptions.DEFAULT);
            for (SearchHit hit : resp.getHits().getHits()) {
                Map<String,Object> result = hit.getSourceAsMap();
                System.out.println("match_ALL-------" + result );
            }
            System.out.println("----------------------------------------------------------------------------------------------------------------------------------" );
        }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值