⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queryparser.jj

📁 lucene-2.4.0 是一个全文收索的工具包
💻 JJ
📖 第 1 页 / 共 3 页
字号:
   * collator using this method will cause every single index Term in the   * Field referenced by lowerTerm and/or upperTerm to be examined.   * Depending on the number of index Terms in this Field, the operation could   * be very slow.   *   *  @param rc  the collator to use when constructing RangeQuerys   *             and ConstantScoreRangeQuerys   */  public void setRangeCollator(Collator rc) {    rangeCollator = rc;  }    /**   * @return the collator used to determine index term inclusion in ranges   *  specified either for ConstantScoreRangeQuerys or RangeQuerys (if   *  {@link #setUseOldRangeQuery(boolean)} is called with a <code>true</code>   *  value.)   */  public Collator getRangeCollator() {    return rangeCollator;  }  /**   * @deprecated use {@link #addClause(List, int, int, Query)} instead.   */  protected void addClause(Vector clauses, int conj, int mods, Query q) {    addClause((List) clauses, conj, mods, q);  }  protected void addClause(List clauses, int conj, int mods, Query q) {    boolean required, prohibited;    // If this term is introduced by AND, make the preceding term required,    // unless it's already prohibited    if (clauses.size() > 0 && conj == CONJ_AND) {      BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);      if (!c.isProhibited())        c.setOccur(BooleanClause.Occur.MUST);    }    if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {      // If this term is introduced by OR, make the preceding term optional,      // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)      // notice if the input is a OR b, first term is parsed as required; without      // this modification a OR b would parsed as +a OR b      BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);      if (!c.isProhibited())        c.setOccur(BooleanClause.Occur.SHOULD);    }    // We might have been passed a null query; the term might have been    // filtered away by the analyzer.    if (q == null)      return;    if (operator == OR_OPERATOR) {      // We set REQUIRED if we're introduced by AND or +; PROHIBITED if      // introduced by NOT or -; make sure not to set both.      prohibited = (mods == MOD_NOT);      required = (mods == MOD_REQ);      if (conj == CONJ_AND && !prohibited) {        required = true;      }    } else {      // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED      // if not PROHIBITED and not introduced by OR      prohibited = (mods == MOD_NOT);      required   = (!prohibited && conj != CONJ_OR);    }    if (required && !prohibited)      clauses.add(newBooleanClause(q, BooleanClause.Occur.MUST));    else if (!required && !prohibited)      clauses.add(newBooleanClause(q, BooleanClause.Occur.SHOULD));    else if (!required && prohibited)      clauses.add(newBooleanClause(q, BooleanClause.Occur.MUST_NOT));    else      throw new RuntimeException("Clause cannot be both required and prohibited");  }  /**   * @exception ParseException throw in overridden method to disallow   */  protected Query getFieldQuery(String field, String queryText)  throws ParseException {    // Use the analyzer to get all the tokens, and then build a TermQuery,    // PhraseQuery, or nothing based on the term count    TokenStream source = analyzer.tokenStream(field, new StringReader(queryText));    List list = new ArrayList();    final org.apache.lucene.analysis.Token reusableToken = new org.apache.lucene.analysis.Token();    org.apache.lucene.analysis.Token nextToken;    int positionCount = 0;    boolean severalTokensAtSamePosition = false;    while (true) {      try {        nextToken = source.next(reusableToken);      }      catch (IOException e) {        nextToken = null;      }      if (nextToken == null)        break;      list.add(nextToken.clone());      if (nextToken.getPositionIncrement() != 0)        positionCount += nextToken.getPositionIncrement();      else        severalTokensAtSamePosition = true;    }    try {      source.close();    }    catch (IOException e) {      // ignore    }    if (list.size() == 0)      return null;    else if (list.size() == 1) {      nextToken = (org.apache.lucene.analysis.Token) list.get(0);      return newTermQuery(new Term(field, nextToken.term()));    } else {      if (severalTokensAtSamePosition) {        if (positionCount == 1) {          // no phrase query:          BooleanQuery q = newBooleanQuery(true);          for (int i = 0; i < list.size(); i++) {            nextToken = (org.apache.lucene.analysis.Token) list.get(i);            Query currentQuery = newTermQuery(                new Term(field, nextToken.term()));            q.add(currentQuery, BooleanClause.Occur.SHOULD);          }          return q;        }        else {          // phrase query:          MultiPhraseQuery mpq = newMultiPhraseQuery();          mpq.setSlop(phraseSlop);          List multiTerms = new ArrayList();          int position = -1;          for (int i = 0; i < list.size(); i++) {            nextToken = (org.apache.lucene.analysis.Token) list.get(i);            if (nextToken.getPositionIncrement() > 0 && multiTerms.size() > 0) {              if (enablePositionIncrements) {                mpq.add((Term[])multiTerms.toArray(new Term[0]),position);              } else {                mpq.add((Term[])multiTerms.toArray(new Term[0]));              }              multiTerms.clear();            }            position += nextToken.getPositionIncrement();            multiTerms.add(new Term(field, nextToken.term()));          }          if (enablePositionIncrements) {            mpq.add((Term[])multiTerms.toArray(new Term[0]),position);          } else {            mpq.add((Term[])multiTerms.toArray(new Term[0]));          }          return mpq;        }      }      else {        PhraseQuery pq = newPhraseQuery();        pq.setSlop(phraseSlop);        int position = -1;        for (int i = 0; i < list.size(); i++) {          nextToken = (org.apache.lucene.analysis.Token) list.get(i);          if (enablePositionIncrements) {            position += nextToken.getPositionIncrement();            pq.add(new Term(field, nextToken.term()),position);          } else {            pq.add(new Term(field, nextToken.term()));          }        }        return pq;      }    }  }  /**   * Base implementation delegates to {@link #getFieldQuery(String,String)}.   * This method may be overridden, for example, to return   * a SpanNearQuery instead of a PhraseQuery.   *   * @exception ParseException throw in overridden method to disallow   */  protected Query getFieldQuery(String field, String queryText, int slop)        throws ParseException {    Query query = getFieldQuery(field, queryText);    if (query instanceof PhraseQuery) {      ((PhraseQuery) query).setSlop(slop);    }    if (query instanceof MultiPhraseQuery) {      ((MultiPhraseQuery) query).setSlop(slop);    }    return query;  }  /**   * @exception ParseException throw in overridden method to disallow   */  protected Query getRangeQuery(String field,                                String part1,                                String part2,                                boolean inclusive) throws ParseException  {    if (lowercaseExpandedTerms) {      part1 = part1.toLowerCase();      part2 = part2.toLowerCase();    }    try {      DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);      df.setLenient(true);      Date d1 = df.parse(part1);      Date d2 = df.parse(part2);      if (inclusive) {        // The user can only specify the date, not the time, so make sure        // the time is set to the latest possible time of that date to really        // include all documents:        Calendar cal = Calendar.getInstance(locale);        cal.setTime(d2);        cal.set(Calendar.HOUR_OF_DAY, 23);        cal.set(Calendar.MINUTE, 59);        cal.set(Calendar.SECOND, 59);        cal.set(Calendar.MILLISECOND, 999);        d2 = cal.getTime();      }      DateTools.Resolution resolution = getDateResolution(field);      if (resolution == null) {        // no default or field specific date resolution has been set,        // use deprecated DateField to maintain compatibilty with        // pre-1.9 Lucene versions.        part1 = DateField.dateToString(d1);        part2 = DateField.dateToString(d2);      } else {        part1 = DateTools.dateToString(d1, resolution);        part2 = DateTools.dateToString(d2, resolution);      }    }    catch (Exception e) { }    return newRangeQuery(field, part1, part2, inclusive);  } /**  * Builds a new BooleanQuery instance  * @param disableCoord disable coord  * @return new BooleanQuery instance  */  protected BooleanQuery newBooleanQuery(boolean disableCoord) {    return new BooleanQuery(disableCoord);   } /**  * Builds a new BooleanClause instance  * @param q sub query  * @param occur how this clause should occur when matching documents  * @return new BooleanClause instance  */  protected BooleanClause newBooleanClause(Query q, BooleanClause.Occur occur) {    return new BooleanClause(q, occur);  }   /**   * Builds a new TermQuery instance   * @param term term   * @return new TermQuery instance   */  protected Query newTermQuery(Term term){    return new TermQuery(term);  }   /**   * Builds a new PhraseQuery instance   * @return new PhraseQuery instance   */  protected PhraseQuery newPhraseQuery(){    return new PhraseQuery();  }   /**   * Builds a new MultiPhraseQuery instance   * @return new MultiPhraseQuery instance   */  protected MultiPhraseQuery newMultiPhraseQuery(){    return new MultiPhraseQuery();  }   /**   * Builds a new PrefixQuery instance   * @param prefix Prefix term   * @return new PrefixQuery instance   */  protected Query newPrefixQuery(Term prefix){    return new PrefixQuery(prefix);  }   /**   * Builds a new FuzzyQuery instance   * @param term Term   * @param minimumSimilarity minimum similarity   * @param prefixLength prefix length   * @return new FuzzyQuery Instance   */  protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {    return new FuzzyQuery(term,minimumSimilarity,prefixLength);  }  /**   * Builds a new RangeQuery instance   * @param field Field   * @param part1 min   * @param part2 max   * @param inclusive true if range is inclusive   * @return new RangeQuery instance   */  protected Query newRangeQuery(String field, String part1, String part2, boolean inclusive) {    if(useOldRangeQuery)    {      return new RangeQuery(new Term(field, part1),                            new Term(field, part2),                            inclusive, rangeCollator);    }    else    {      return new ConstantScoreRangeQuery        (field, part1, part2, inclusive, inclusive, rangeCollator);    }  }  /**   * Builds a new MatchAllDocsQuery instance   * @return new MatchAllDocsQuery instance   */  protected Query newMatchAllDocsQuery() {    return new MatchAllDocsQuery();   }  /**   * Builds a new WildcardQuery instance   * @param t wildcard term   * @return new WildcardQuery instance   */  protected Query newWildcardQuery(Term t) {    return new WildcardQuery(t);   }  /**   * Factory method for generating query, given a set of clauses.   * By default creates a boolean query composed of clauses passed in.   *   * Can be overridden by extending classes, to modify query being   * returned.   *   * @param clauses List that contains {@link BooleanClause} instances   *    to join.   *   * @return Resulting {@link Query} object.   * @exception ParseException throw in overridden method to disallow   * @deprecated use {@link #getBooleanQuery(List)} instead   */  protected Query getBooleanQuery(Vector clauses) throws ParseException {    return getBooleanQuery((List) clauses, false);  }  /**   * Factory method for generating query, given a set of clauses.   * By default creates a boolean query composed of clauses passed in.   *   * Can be overridden by extending classes, to modify query being   * returned.   *   * @param clauses List that contains {@link BooleanClause} instances   *    to join.   *   * @return Resulting {@link Query} object.   * @exception ParseException throw in overridden method to disallow   */  protected Query getBooleanQuery(List clauses) throws ParseException {    return getBooleanQuery(clauses, false);  }  /**   * Factory method for generating query, given a set of clauses.   * By default creates a boolean query composed of clauses passed in.   *   * Can be overridden by extending classes, to modify query being   * returned.   *   * @param clauses List that contains {@link BooleanClause} instances   *    to join.   * @param disableCoord true if coord scoring should be disabled.   *   * @return Resulting {@link Query} object.   * @exception ParseException throw in overridden method to disallow   * @deprecated use {@link #getBooleanQuery(List, boolean)} instead   */  protected Query getBooleanQuery(Vector clauses, boolean disableCoord)    throws ParseException  {    return getBooleanQuery((List) clauses, disableCoord);  }  /**   * Factory method for generating query, given a set of clauses.   * By default creates a boolean query composed of clauses passed in.   *   * Can be overridden by extending classes, to modify query being   * returned.   *   * @param clauses List that contains {@link BooleanClause} instances   *    to join.   * @param disableCoord true if coord scoring should be disabled.   *   * @return Resulting {@link Query} object.   * @exception ParseException throw in overridden method to disallow   */  protected Query getBooleanQuery(List clauses, boolean disableCoord)    throws ParseException  {    if (clauses.size()==0) {      return null; // all clause words were filtered away by the analyzer.    }    BooleanQuery query = newBooleanQuery(disableCoord);    for (int i = 0; i < clauses.size(); i++) {      query.add((BooleanClause)clauses.get(i));    }    return query;  }  /**   * Factory method for generating a query. Called when parser   * parses an input term token that contains one or more wildcard   * characters (? and *), but is not a prefix term token (one   * that has just a single * character at the end)   *<p>   * Depending on settings, prefix term may be lower-cased   * automatically. It will not go through the default Analyzer,   * however, since normal Analyzers are unlikely to work properly   * with wildcard templates.   *<p>   * Can be overridden by extending classes, to provide custom handling for   * wildcard queries, which may be necessary due to missing analyzer calls.   *   * @param field Name of the field query will use.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -