Exemplo n.º 1
0
 /**
  * 生成多字段查询对象。
  *
  * @param query 查询条件
  * @param fieldNames 查询字段
  * @return 返回多字段查询对象。
  */
 public Query generateMultiFieldQuery(String query, String[] fieldNames) {
   Assert.notEmpty(fieldNames, "必须指定查询的字段。");
   Map<String, Analyze> fields = new LinkedHashMap<String, Analyze>();
   for (String fieldName : fieldNames) {
     fields.put(fieldName, searchFields.get(fieldName));
   }
   return generateMultiFieldQuery(query, fields);
 }
Exemplo n.º 2
0
 /**
  * 生成多字段查询对象。
  *
  * @param query 查询条件
  * @param fields 查询字段
  * @return 返回多字段查询对象。
  */
 public Query generateMultiFieldQuery(String query, Map<String, Analyze> fields) {
   Assert.notEmpty(fields, "必须指定查询的字段。");
   BooleanQuery multiFieldWildcardQuery = new BooleanQuery();
   for (Entry<String, Analyze> field : fields.entrySet()) {
     if (field.getValue() == Analyze.NO) {
       Term term = new Term(field.getKey(), "*" + query + "*");
       WildcardQuery fuzzyQuery = new WildcardQuery(term);
       multiFieldWildcardQuery.add(fuzzyQuery, Occur.SHOULD);
     } else {
       QueryParser parser =
           new QueryParser(
               Version.LUCENE_36, field.getKey(), session.getSearchFactory().getAnalyzer(clazz));
       parser.setPhraseSlop(0);
       parser.setAutoGeneratePhraseQueries(true);
       try {
         multiFieldWildcardQuery.add(parser.parse(query), Occur.SHOULD);
       } catch (ParseException e) {
         throw new HibernateException("生成多字段查询时发生异常", e);
       }
     }
   }
   return multiFieldWildcardQuery;
 }