elasticsearch 多条件查询 - 不包含关键字查询 must_not
方式1 restful api
不包含指定关键字 使用 must_not
POST http://192.168.199.126:9200/film/dongzuo/_search/
{
"query": {
"bool": {
"must": {
"match": {
"title": "战"
}
},
"must_not": {
"match": {
"content": "武士"
}
}
}
}
}

方式2 Java接口实现
部分代码
private TransportClient client;
private static String host="192.168.199.126";
private static int port=9300; // 程序连接的端口
// 配置settings 集群相关
private static Settings.Builder settings = Settings.builder().put("cluster.name","my-application");
@Before
public void init() throws UnknownHostException {
client = new PreBuiltTransportClient(settings.build())
.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
}
@After
public void close(){
if(client!=null){
client.close();
}
}
/**
* 多条件查询 包含与不包含某个关键词
* @throws Exception
*/
@Test
public void searchMutil2()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "武士");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder) // 包含
.mustNot(queryBuilder2))// 不包含
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
