使用Lucene做全文检索,一般我们经常会在多个字段(域)中查找,而不一定关心在那个字段中包含需要查找的值.比如在搜索框中输入:亲亲宝宝 软件开发,只要标题、内容、作者等包含“亲亲宝宝 软件开发”都是我们要查找的结果。Lucene中MultiFieldQueryParser正好给我们提供多字段查找带来方便.

MultiFieldQueryParser multiParser=

new MultiFieldQueryParser(Version.LUCENE_CURRENT

,new String[]{"title","content","author"},analyzer);

multiParser.setPhraseSlop(3); 
//设置短语搜索的坡度为3,默认为0

multiParser.setDefaultOperator(QueryParser.Operator.AND);

//设置以空格分开的短语是并的关系,默认为或的关系;

Query query=multiParser.parse(str); 

但是我使用的lucene3.0的MultiFieldQueryParser有点问题,就是setPhraseSlop后不能产生作用,在源代码的136行修改0为 this.getPhraseSlop()即可.

 protected Query getFieldQuery(String field, String queryText) throws ParseException {

    return getFieldQuery(field, queryText, this.getPhraseSlop());
 }

相关文章:

  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2021-07-02
  • 2021-10-12
猜你喜欢
  • 2022-01-23
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
相关资源
相似解决方案