Lucene 单域多条件查询

在Lucene 中 BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表示not,BooleanClause.Occur.SHOULD表示or。

下面给出个例子

  1. **  
  2. * 根据信息分类和关键词进行查询  
  3. * @param type,资源的类型,其值为news或product  
  4. * @param searchKey,搜索的关键字  
  5. * @return Hits  
  6. */  
  7. public Hits executeSearch(String keyword)   
  8. {   
  9.   Hits result = null;   
  10.   if(keyword != null && !keyword.equals(""))   
  11.   {   
  12.     try    
  13.     {   
  14.       //根据关键字构造一个数组   
  15.       String[] key = new String[]{keyword,type};   
  16.       //同时声明一个与之对应的字段数组   
  17.       String[] fields = {"title"};   
  18.       //声明BooleanClause.Occur[]数组,它表示多个条件之间的关系   
  19.       BooleanClause.Occur[] flags=new BooleanClause.Occur[]{BooleanClause.Occur.MUST,BooleanClause.Occur.MUST};   
  20.       ChineseAnalyzer analyzer = new ChineseAnalyzer();   
  21.       //用MultiFieldQueryParser得到query对象   
  22.       Query query = MultiFieldQueryParser.parse(key, fields, flags, analyzer);   
  23.       //c:/index表示我们的索引文件所在的目录   
  24.       IndexSearcher searcher = new IndexSearcher("c:/index");   
  25.       //查询结果   
  26.       result = searcher.search(query);   
  27.     } catch (Exception e)   
  28.     {   
  29.       e.printStackTrace();   
  30.     }   
  31.   }   
  32.   return result;   
  33. }  

  多条件索引关系

BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST,

BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.SHOULD。有以下6种组合: 
1.MUST和MUST:取得连个查询子句的交集。 
2.MUST和MUST_NOT:表示查询结果中不能包含MUST_NOT所对应得查询子句的检索结果。 
3.MUST_NOT和MUST_NOT:无意义,检索无结果。 
4.SHOULD与MUST、SHOULD与MUST_NOT:SHOULD与MUST连用时,无意义,结果为MUST子句的检索

结果。与MUST_NOT连用时,功能同MUST。 
5.SHOULD与SHOULD:表示“或”关系,最终检索结果为所有检索子句的并集。

原文地址:https://www.cnblogs.com/dingjiaoyang/p/6115358.html