コード例 #1
0
  /* (non-Javadoc)
   * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
   */
  @Override
  public Query getQuery(Element e) throws ParserException {
    String fieldsList = e.getAttribute("fieldNames"); // a comma-delimited list of fields
    String fields[] = defaultFieldNames;
    if ((fieldsList != null) && (fieldsList.trim().length() > 0)) {
      fields = fieldsList.trim().split(",");
      // trim the fieldnames
      for (int i = 0; i < fields.length; i++) {
        fields[i] = fields[i].trim();
      }
    }

    // Parse any "stopWords" attribute
    // TODO MoreLikeThis needs to ideally have per-field stopWords lists - until then
    // I use all analyzers/fields to generate multi-field compatible stop list
    String stopWords = e.getAttribute("stopWords");
    Set<String> stopWordsSet = null;
    if ((stopWords != null) && (fields != null)) {
      stopWordsSet = new HashSet<String>();
      for (String field : fields) {
        try (TokenStream ts = analyzer.tokenStream(field, stopWords)) {
          CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
          ts.reset();
          while (ts.incrementToken()) {
            stopWordsSet.add(termAtt.toString());
          }
          ts.end();
          ts.close();
        } catch (IOException ioe) {
          throw new ParserException(
              "IoException parsing stop words list in "
                  + getClass().getName()
                  + ":"
                  + ioe.getLocalizedMessage());
        }
      }
    }

    MoreLikeThisQuery mlt = new MoreLikeThisQuery(DOMUtils.getText(e), fields, analyzer, fields[0]);
    mlt.setMaxQueryTerms(DOMUtils.getAttribute(e, "maxQueryTerms", DEFAULT_MAX_QUERY_TERMS));
    mlt.setMinTermFrequency(
        DOMUtils.getAttribute(e, "minTermFrequency", DEFAULT_MIN_TERM_FREQUENCY));
    mlt.setPercentTermsToMatch(
        DOMUtils.getAttribute(e, "percentTermsToMatch", DEFAULT_PERCENT_TERMS_TO_MATCH) / 100);
    mlt.setStopWords(stopWordsSet);
    int minDocFreq = DOMUtils.getAttribute(e, "minDocFreq", -1);
    if (minDocFreq >= 0) {
      mlt.setMinDocFreq(minDocFreq);
    }

    mlt.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));

    return mlt;
  }
 @Override
 public Query getQuery(Element e) throws ParserException {
   String text = DOMUtils.getText(e);
   try {
     Query q = null;
     if (unSafeParser != null) {
       // synchronize on unsafe parser
       synchronized (unSafeParser) {
         q = unSafeParser.parse(text);
       }
     } else {
       String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
       // Create new parser
       QueryParser parser = createQueryParser(fieldName, analyzer);
       q = parser.parse(text);
     }
     q.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
     return q;
   } catch (ParseException e1) {
     throw new ParserException(e1.getMessage());
   }
 }