elasticsearch在should和must查詢時不能精確查出數據,主要原因是在7.0版本后should查詢時minimum_should_match默認為0,查出了非should條件中的數據。
minimum_should_match可以控制查詢精度,在should和must聯合查詢查詢時必須使用,否則查不出精確的數據。
{ "size":20, "query":{ "bool":{ "should":[ { "match_phrase":{ "Title":"關鍵詞" } }, { "match_phrase":{ "summary":"關鍵詞" } }, { "match_phrase":{ "Content":"關鍵詞" } } ], "minimum_should_match":1, "must":{ "match_phrase":{ "categoryId":0 } } } } }
minimum_should_match為1時,表示無管should有多少可選條件子句,至少滿足1個條件。
minimum_should_match為-1時,負數時,至少滿足should可選子句的總數減去此數字應該是必需的。
如果不使用minimum_should_match,還有一種解決方案。
(categoryId=0&Title="關鍵詞")||(categoryId=0&summary="關鍵詞")||(categoryId=0&Content="關鍵詞")
{ "size":20, "query":{ "bool":{ "should":[ { "bool":{ "must":[ { "match_phrase":{ "categoryId":0 } }, { "match_phrase":{ "Title":"關鍵詞" } } ] } }, { "bool":{ "must":[ { "match_phrase":{ "categoryId":0 } }, { "match_phrase":{ "summary":"關鍵詞" } } ] } }, { "bool":{ "must":[ { "match_phrase":{ "categoryId":0 } }, { "match_phrase":{ "Content":"關鍵詞" } } ] } } ] } } }