ES Java API_对汽车品牌进行多种条件的组合搜索

2018-02-27 11:56:01来源:oschina作者:jxlgzwh人点击

分享

import java.net.InetAddress;


import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.transport.client.PreBuiltTransportClient;


public class BoolQuerySearchBrand {@SuppressWarnings({ "resource", "unchecked" })public static void main(String[] args) throws Exception { Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .build();TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.matchQuery("brand", "宝马")) .mustNot(QueryBuilders.termQuery("name.raw", "宝马318")) .should(QueryBuilders.rangeQuery("produce_date").gte("2017-01-01").lte("2017-01-31")) .filter(QueryBuilders.rangeQuery("price").gte(280000).lte(350000)); SearchResponse searchResponse = client.prepareSearch("car_shop").setTypes("cars") .setQuery(queryBuilder) .get();for(SearchHit searchHit : searchResponse.getHits().getHits()) {System.out.println(searchHit.getSourceAsString());}client.close();} }

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台