elasticsearchElasticsearchES搜索系列

Elasticsearch查询API的两种实现

2018-04-11  本文已影响50人  代码足迹

1.引入pom文件

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.4.5</version>
        </dependency>

此处用了2.4.5的版本

2.输入一个值在多个字段里面匹配

比如:表里面有三个字段 姓名name,地址address,备注remark,那么我输入一个值只要任何一个字段匹配即可


String esClusterName = "bigdata";
String esNodes = "172.16.7.14";
int esPort = 9306;

Settings settings = Settings.settingsBuilder().put("cluster.name", esClusterName) // 设置集群名
        .put("client.transport.ignore_cluster_name", true) // 忽略集群名字验证, 打开后集群名字不对也能连接上
        .build();

TransportClient client = TransportClient.builder().settings(settings).build()
        .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(esNodes, esPort)));

//构建查询Builder
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("userinfo").setTypes("type");

//查询条件
String queryString = "石头";
QueryStringQueryBuilder qsBuilder = QueryBuilders.queryStringQuery(queryString);
qsBuilder.field("name").field("address").field("remark");// name,address,remark中只要有一个符合均合条件 

BoolQueryBuilder searchBuilder = QueryBuilders.boolQuery();
searchBuilder.filter(qsBuilder);


searchRequestBuilder.setQuery(searchBuilder);

//执行查询
SearchResponse response = searchRequestBuilder.execute().actionGet();

//输出结果
SearchHits hits = response.getHits();
for (SearchHit hit : hits.getHits()) {
    System.out.println(hit.sourceAsMap());
}

client.close();

3.每个字段各自匹配

比如:表里面有三个字段 姓名name,地址address,备注remark
需要找 name为张三,address是南山 remark为外国人


String esClusterName = "bigdata";
String esNodes = "172.16.7.14";
int esPort = 9306;

Settings settings = Settings.settingsBuilder().put("cluster.name", esClusterName) // 设置集群名
        .put("client.transport.ignore_cluster_name", true) // 忽略集群名字验证, 打开后集群名字不对也能连接上
        .build();

TransportClient client = TransportClient.builder().settings(settings).build()
        .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(esNodes, esPort)));

//构建查询Builder
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("info_case").setTypes("type");

//查询:每个字段给不同的值,三个查询条件 
QueryBuilder builder = QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("name", "张三")));
QueryBuilder builder1 = QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("address", "南山")));
QueryBuilder builder2 = QueryBuilders.boolQuery().should(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("remark", "外国人")));

searchRequestBuilder.setQuery(builder).setQuery(builder1).setQuery(builder2);

//排序
SortBuilder sortBuilder = SortBuilders.fieldSort("CREATETIME").order(SortOrder.DESC);
searchRequestBuilder.addSort(sortBuilder);

//开始查询
SearchResponse response = searchRequestBuilder.execute().actionGet();
SearchHits hits = response.getHits();
for (SearchHit hit : hits.getHits()) {
    System.out.println(hit.sourceAsMap());
}
上一篇下一篇

猜你喜欢

热点阅读