ElasticSearch Java API 官网:
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/getting-started.html
ElasticSearch 过滤条件,类似于SQL中的and、or、not
demo如下:
- //搜索内容设置>>>>>>>>>>>>>>>>>>>>>>
- //索引"student_manage"是索引"student"、"class"、"course_info"共同的别名
- SearchRequestBuilder searchRequestBuilder = client.prepareSearch("student_manage");
- //searchRequestBuilder.setTypes(type);
- searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
- ......
- //开始过滤>>>>>>>>>>>>>>>>>>>>>>
- //************过滤条件中,must相当于and,should相当于or,must_not相当于not*****************//
- //过滤条件1
- org.elasticsearch.index.query.QueryBuilder postFilter1= QueryBuilders
- .boolQuery()
- .must(QueryBuilders.termsQuery("_index", "student"))
- .must(QueryBuilders.termsQuery("sex", "male"))
- .must(QueryBuilders.termsQuery("age", "16","17"));
- //过滤条件2
- org.elasticsearch.index.query.QueryBuilder postFilter2= QueryBuilders
- .boolQuery()
- .must(QueryBuilders.termsQuery("_index", "class"))
- .must(QueryBuilders.termsQuery("type", "math"))
- .must(QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("num", "6","8")));
- //过滤条件3
- org.elasticsearch.index.query.QueryBuilder postFilter3= QueryBuilders
- .boolQuery()
- .must(QueryBuilders.termsQuery("_index", "course_info"))
- .must(QueryBuilders.termsQuery("isopen", "1"))
- .must(QueryBuilders.termsQuery("status", "2"))
- .must(QueryBuilders.termsQuery("grade", "2"));
- //整合三者过滤条件(OR)
- org.elasticsearch.index.query.QueryBuilder postFilterBool =QueryBuilders.boolQuery()
- .should(postFilter1)
- .should(postFilter2)
- .should(postFilter3);
- searchRequestBuilder.setPostFilter(postFilterBool);
- //执行搜索>>>>>>>>>>>>>>>>>>>>>>
- SearchResponse res = searchRequestBuilder.execute().actionGet();