org.apache.lucene.search.BooleanQuery.clauses()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中org.apache.lucene.search.BooleanQuery.clauses()方法的一些代码示例,展示了BooleanQuery.clauses()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BooleanQuery.clauses()方法的具体详情如下:
包路径:org.apache.lucene.search.BooleanQuery
类名称:BooleanQuery
方法名:clauses

BooleanQuery.clauses介绍

[英]Return a list of the clauses of this BooleanQuery.
[中]返回此布尔查询的子句列表。

代码示例

代码示例来源:origin: oracle/opengrok

/**
 * Decomposes the provided {@code query} into terms with the exception of {@link PhraseQuery}. Is useful when
 * determining which terms should not be suggested. {@link PhraseQuery} is exempted because not suggesting some
 * term which were contained in it is invalid.
 * @param query query to decompose
 * @return terms that were in the {@code query}
 */
public static List<Term> intoTermsExceptPhraseQuery(final Query query) {
  if (query == null) {
    return Collections.emptyList();
  }
  List<Term> terms = new LinkedList<>();
  LinkedList<Query> queue = new LinkedList<>();
  queue.add(query);
  while (!queue.isEmpty()) {
    Query q = queue.poll();
    if (q instanceof BooleanQuery) {
      for (BooleanClause bc : ((BooleanQuery) q).clauses()) {
        queue.add(bc.getQuery());
      }
    } else if (q instanceof TermQuery) {
      terms.add(((TermQuery) q).getTerm());
    }
  }
  return terms;
}

代码示例来源:origin: org.apache.lucene/lucene-core

private static boolean shouldNeverCache(Query query) {
 if (query instanceof TermQuery) {
  // We do not bother caching term queries since they are already plenty fast.
  return true;
 }
 if (query instanceof MatchAllDocsQuery) {
  // MatchAllDocsQuery has an iterator that is faster than what a bit set could do.
  return true;
 }
 // For the below queries, it's cheap to notice they cannot match any docs so
 // we do not bother caching them.
 if (query instanceof MatchNoDocsQuery) {
  return true;
 }
 if (query instanceof BooleanQuery) {
  BooleanQuery bq = (BooleanQuery) query;
  if (bq.clauses().isEmpty()) {
   return true;
  }
 }
 if (query instanceof DisjunctionMaxQuery) {
  DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) query;
  if (dmq.getDisjuncts().isEmpty()) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: oracle/opengrok

private boolean needPositionsAndFrequencies(final Query query) {
  if (query instanceof CustomPhraseQuery) {
    return true;
  }
  if (query instanceof BooleanQuery) {
    for (BooleanClause bc : ((BooleanQuery) query).clauses()) {
      if (needPositionsAndFrequencies(bc.getQuery())) {
        return true;
      }
    }
  }
  return false;
}

代码示例来源:origin: oracle/opengrok

/**
 * Decomposes the provided {@code query} into terms.
 * @param query query to decompose
 * @return terms that were in the {@code query}
 */
public static List<Term> intoTerms(final Query query) {
  if (query == null) {
    return Collections.emptyList();
  }
  List<Term> terms = new LinkedList<>();
  LinkedList<Query> queue = new LinkedList<>();
  queue.add(query);
  while (!queue.isEmpty()) {
    Query q = queue.poll();
    if (q instanceof BooleanQuery) {
      for (BooleanClause bc : ((BooleanQuery) q).clauses()) {
        queue.add(bc.getQuery());
      }
    } else if (q instanceof TermQuery) {
      terms.add(((TermQuery) q).getTerm());
    } else if (q instanceof PhraseQuery) {
      terms.addAll(Arrays.asList(((PhraseQuery) q).getTerms()));
    }
  }
  return terms;
}

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Builds a new GraphQuery for multi-terms synonyms.
 * <p>
 * This is intended for subclasses that wish to customize the generated queries.
 * @return new Query instance
 */
protected Query newGraphSynonymQuery(Iterator<Query> queries) {
 BooleanQuery.Builder builder = new BooleanQuery.Builder();
 while (queries.hasNext()) {
  builder.add(queries.next(), BooleanClause.Occur.SHOULD);
 }
 BooleanQuery bq = builder.build();
 if (bq.clauses().size() == 1) {
  return bq.clauses().get(0).getQuery();
 }
 return bq;
}

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Rebuilds a boolean query and sets a new minimum number should match value.
 */
private BooleanQuery addMinShouldMatchToBoolean(BooleanQuery query, float fraction) {
 BooleanQuery.Builder builder = new BooleanQuery.Builder();
 builder.setMinimumNumberShouldMatch((int) (fraction * query.clauses().size()));
 for (BooleanClause clause : query) {
  builder.add(clause);
 }
 return builder.build();
}

代码示例来源:origin: org.apache.lucene/lucene-core

int shouldMatchCount = 0;
Iterator<Weight> wIt = weights.iterator();
Iterator<BooleanClause> cIt = query.clauses().iterator();
while (wIt.hasNext()) {
 Weight w = wIt.next();

代码示例来源:origin: neo4j/neo4j

private Query injectOrphans( Query query )
{
  if ( query instanceof BooleanQuery )
  {
    BooleanQuery source = (BooleanQuery) query;
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for ( BooleanClause clause : source.clauses() )
    {
      builder.add( injectOrphans( clause.getQuery() ), clause.getOccur() );
    }
    return builder.build();
  }
  String orphanField = extractTermField( query );
  if ( orphanField == null )
  {
    return query;
  }
  return new BooleanQuery.Builder()
      .add( query, Occur.SHOULD )
      .add( new TermQuery( new Term( ORPHANS_KEY, orphanField ) ), Occur.SHOULD )
      .build();
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldBuildQueryRepresentingBoolProperty()
{
  // given
  BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery( true );
  ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 0 ).getQuery();
  TermQuery query = (TermQuery) constantScoreQuery.getQuery();
  // then
  assertEquals( "true", query.getTerm().text() );
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldBuildQueryRepresentingStringProperty()
{
  // given
  BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery( "Characters" );
  ConstantScoreQuery query = (ConstantScoreQuery) booleanQuery.clauses().get( 0 ).getQuery();
  // then
  assertEquals( "Characters", ((TermQuery) query.getQuery()).getTerm().text() );
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldBuildQueryRepresentingArrayProperty()
{
  // given
  BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery( new Object[]{new Integer[]{1, 2, 3}} );
  ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 0 ).getQuery();
  TermQuery query = (TermQuery) constantScoreQuery.getQuery();
  // then
  assertEquals( "D1.0|2.0|3.0|", query.getTerm().text() );
}

代码示例来源:origin: neo4j/neo4j

@SuppressWarnings( "unchecked" )
@Test
void shouldBuildQueryRepresentingNumberProperty()
{
  // given
  BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery( 12 );
  ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 0 ).getQuery();
  NumericRangeQuery<Double> query = (NumericRangeQuery<Double>) constantScoreQuery.getQuery();
  // then
  assertEquals( 12.0, query.getMin(), 0.001 );
  assertEquals( 12.0, query.getMax(),0.001 );
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldBuildQueryRepresentingMultipleProperties()
{
  // given
  BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery( true, "Characters", 12, new Integer[]{1, 2, 3} );
  ConstantScoreQuery boolScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 0 ).getQuery();
  TermQuery boolTermQuery = (TermQuery) boolScoreQuery.getQuery();
  ConstantScoreQuery stringScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 1 ).getQuery();
  TermQuery stringTermQuery = (TermQuery) stringScoreQuery.getQuery();
  ConstantScoreQuery numberScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 2 ).getQuery();
  NumericRangeQuery<Double> numericRangeQuery = (NumericRangeQuery<Double>) numberScoreQuery.getQuery();
  ConstantScoreQuery arrayScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get( 3 ).getQuery();
  TermQuery arrayTermQuery = (TermQuery) arrayScoreQuery.getQuery();
  // then
  assertEquals( "true", boolTermQuery.getTerm().text() );
  assertEquals( "Characters", stringTermQuery.getTerm().text() );
  assertEquals( 12.0, numericRangeQuery.getMin(), 0.001 );
  assertEquals( 12.0, numericRangeQuery.getMax(), 0.001 );
  assertEquals( "D1.0|2.0|3.0|", arrayTermQuery.getTerm().text() );
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private static boolean isNegativeQuery(Query q) {
  if (!(q instanceof BooleanQuery)) {
    return false;
  }
  List<BooleanClause> clauses = ((BooleanQuery) q).clauses();
  if (clauses.isEmpty()) {
    return false;
  }
  for (BooleanClause clause : clauses) {
    if (!clause.isProhibited()) return false;
  }
  return true;
}

代码示例来源:origin: rnewson/couchdb-lucene

private static void planBooleanQuery(final StringBuilder builder, final BooleanQuery query) {
  for (final BooleanClause clause : query.clauses()) {
    builder.append(clause.getOccur());
    toPlan(builder, clause.getQuery());
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private Query boolToExtendedCommonTermsQuery(BooleanQuery bq, Occur highFreqOccur, Occur lowFreqOccur, float
    maxTermFrequency, MappedFieldType fieldType) {
    ExtendedCommonTermsQuery query = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, maxTermFrequency,
      fieldType);
    for (BooleanClause clause : bq.clauses()) {
      if (!(clause.getQuery() instanceof TermQuery)) {
        return bq;
      }
      query.add(((TermQuery) clause.getQuery()).getTerm());
    }
    return query;
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
  BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
  addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST);
  addBooleanClauses(context, booleanQueryBuilder, mustNotClauses, BooleanClause.Occur.MUST_NOT);
  addBooleanClauses(context, booleanQueryBuilder, shouldClauses, BooleanClause.Occur.SHOULD);
  addBooleanClauses(context, booleanQueryBuilder, filterClauses, BooleanClause.Occur.FILTER);
  BooleanQuery booleanQuery = booleanQueryBuilder.build();
  if (booleanQuery.clauses().isEmpty()) {
    return new MatchAllDocsQuery();
  }
  final String minimumShouldMatch;
  if (context.isFilter() && this.minimumShouldMatch == null && shouldClauses.size() > 0) {
    if (mustClauses.size() > 0 || mustNotClauses.size() > 0 || filterClauses.size() > 0) {
      deprecationLogger.deprecatedAndMaybeLog("filter_context_min_should_match",
        "Should clauses in the filter context will no longer automatically set the minimum should " +
          "match to 1 in the next major version. You should group them in a [filter] clause or explicitly set " +
          "[minimum_should_match] to 1 to restore this behavior in the next major version." );
    }
    minimumShouldMatch = "1";
  } else {
    minimumShouldMatch = this.minimumShouldMatch;
  }
  Query query = Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch);
  return adjustPureNegative ? fixNegativeQueryIfNeeded(query) : query;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

if (low.clauses().isEmpty()) {
  BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
  for (BooleanClause booleanClause : high) {
} else if (high.clauses().isEmpty()) {
  return low;
} else {

代码示例来源:origin: org.elasticsearch/elasticsearch

public static Query applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) {
  if (minimumShouldMatch == null) {
    return query;
  }
  int optionalClauses = 0;
  for (BooleanClause c : query.clauses()) {
    if (c.getOccur() == BooleanClause.Occur.SHOULD) {
      optionalClauses++;
    }
  }
  int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch);
  if (0 < msm) {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (BooleanClause clause : query) {
      builder.add(clause);
    }
    builder.setMinimumNumberShouldMatch(msm);
    return builder.build();
  } else {
    return query;
  }
}

代码示例来源:origin: linkedin/indextank-engine

private QueryNode internalParse(org.apache.lucene.search.Query luceneQuery, final String originalStr) throws ParseException {
  QueryNode node;
  if (luceneQuery instanceof org.apache.lucene.search.TermQuery) {
    Term t = ((org.apache.lucene.search.TermQuery) luceneQuery).getTerm();
    String field = t.field();
    String text = t.text();
    node = new TermQuery(field, text);
  } else if (luceneQuery instanceof org.apache.lucene.search.PrefixQuery) {
    Term t = ((org.apache.lucene.search.PrefixQuery) luceneQuery).getPrefix();
    String field = t.field();
    String text = t.text();
    node = new PrefixTermQuery(field, text);
  } else if (luceneQuery instanceof org.apache.lucene.search.BooleanQuery) {
    List<BooleanClause> clauses = ((org.apache.lucene.search.BooleanQuery) luceneQuery).clauses();
    if (clauses.isEmpty()) {
      throw new ParseException("error parsing: " + originalStr);
    }
    node = internalParseBooleanQuery(clauses, originalStr);
  } else if (luceneQuery instanceof org.apache.lucene.search.PhraseQuery) {
    org.apache.lucene.search.PhraseQuery phraseQuery = (org.apache.lucene.search.PhraseQuery) luceneQuery;
    int[] positions = phraseQuery.getPositions();
    node = internalParsePhraseQuery(phraseQuery.getTerms(), positions, originalStr);
  } else {
    throw new ParseException("unimplemented");
  }
  node.setBoost(luceneQuery.getBoost());
  return node;
}

相关文章