org.apache.lucene.search.BooleanQuery类的使用及代码示例

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

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

BooleanQuery介绍

[英]A Query that matches documents matching boolean combinations of other queries, e.g. TermQuerys, PhraseQuerys or other BooleanQuerys.
[中]一种查询,用于匹配与其他查询的布尔组合相匹配的文档,例如termquery、phrasequery或其他booleanquery。

代码示例

代码示例来源:origin: apache/nifi

private BooleanQuery buildLineageQuery(final Collection<String> flowFileUuids) {
  // Create a query for all Events related to the FlowFiles of interest. We do this by adding all ID's as
  // "SHOULD" clauses and then setting the minimum required to 1.
  final BooleanQuery lineageQuery;
  if (flowFileUuids == null || flowFileUuids.isEmpty()) {
    lineageQuery = null;
  } else {
    lineageQuery = new BooleanQuery();
    for (final String flowFileUuid : flowFileUuids) {
      lineageQuery.add(new TermQuery(new Term(SearchableFields.FlowFileUUID.getSearchableFieldName(), flowFileUuid)), Occur.SHOULD);
    }
    lineageQuery.setMinimumNumberShouldMatch(1);
  }
  return lineageQuery;
}

代码示例来源: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: querydsl/querydsl

/**
 * If the query is a BooleanQuery and it contains a single Occur.MUST_NOT
 * clause it will be returned as is. Otherwise it will be wrapped in a
 * BooleanClause with the given Occur.
 */
private BooleanClause createBooleanClause(Query query, Occur occur) {
  if (query instanceof BooleanQuery) {
    BooleanClause[] clauses = ((BooleanQuery) query).getClauses();
    if (clauses.length == 1 && clauses[0].getOccur().equals(Occur.MUST_NOT)) {
      return clauses[0];
    }
  }
  return new BooleanClause(query, occur);
}

代码示例来源:origin: stackoverflow.com

BooleanQuery booleanQuery = new BooleanQuery();

TermQuery userQuery = new TermQuery(new Term("user_id", u.getId()+""));

BooleanQuery innerBooleanQuery = new BooleanQuery();
innerBooleanQuery.add(new BooleanClause(name_query, Occur.SHOULD));
innerBooleanQuery.add(new BooleanClause(desc_query, Occur.SHOULD));

booleanQuery.add(new BooleanClause(userQuery , Occur.MUST));
booleanQuery.add(new BooleanClause(innerBooleanQuery, Occur.MUST));

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

private Filter getFilter() {
  if (filter == null && !filters.isEmpty()) {
    BooleanQuery filterQuery = new BooleanQuery();
    for (Filter filter : filters) {
      filterQuery.add(filter, Occur.SHOULD);
    }
    filter = new QueryWrapperFilter(filterQuery);
  }
  return filter;
}

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

protected Query stringContains(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
  verifyArguments(operation);
  Path<?> path = getPath(operation.getArg(0));
  String field = toField(path);
  String[] terms = convertEscaped(path, operation.getArg(1), metadata);
  if (terms.length > 1) {
    BooleanQuery bq = new BooleanQuery();
    for (String s : terms) {
      bq.add(new WildcardQuery(new Term(field, "*" + s + "*")), Occur.MUST);
    }
    return bq;
  }
  return new WildcardQuery(new Term(field, "*" + terms[0] + "*"));
}

代码示例来源:origin: kzwang/elasticsearch-image

@Override
public Weight createWeight(IndexSearcher searcher) throws IOException {
  IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader());
  indexSearcher.setSimilarity(new SimpleSimilarity());
  BooleanQuery booleanQuery = new BooleanQuery();
  for (int h : hashes) {
    booleanQuery.add(new BooleanClause(new TermQuery(new Term(hashFieldName, Integer.toString(h))), BooleanClause.Occur.SHOULD));
  }
  TopDocs topDocs = indexSearcher.search(booleanQuery, maxResult);
  if (topDocs.scoreDocs.length == 0) {  // no result find
    return Queries.newMatchNoDocsQuery().createWeight(searcher);
  }
  BitSet bitSet = new BitSet(topDocs.scoreDocs.length);
  for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
    bitSet.set(scoreDoc.doc);
  }
  return new ImageHashLimitWeight(searcher, bitSet);
}

代码示例来源:origin: apache/nifi

flowFileIdQuery = null;
} else {
  flowFileIdQuery = new BooleanQuery();
  for (final String flowFileUuid : flowFileUuids) {
    flowFileIdQuery.add(new TermQuery(new Term(SearchableFields.FlowFileUUID.getSearchableFieldName(), flowFileUuid)), Occur.SHOULD);
  flowFileIdQuery.setMinimumNumberShouldMatch(1);
final TopDocs uuidQueryTopDocs = searcher.getIndexSearcher().search(flowFileIdQuery, MAX_QUERY_RESULTS);
final long searchEnd = System.nanoTime();
final Set<ProvenanceEventRecord> recs = docsToEventConverter.convert(uuidQueryTopDocs, searcher.getIndexSearcher().getIndexReader());

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

public Iterable<LuceneDoc> getChildren() {
    //Perform a query for immediate child nodes at given path
    BooleanQuery bq = new BooleanQuery();
    bq.add(new BooleanClause(new TermQuery(newAncestorTerm(path)), BooleanClause.Occur.MUST));
    bq.add(new BooleanClause(newDepthQuery(path), BooleanClause.Occur.MUST));
    try {
      TopDocs docs = sc.searcher.search(bq, Integer.MAX_VALUE);
      return getLuceneDocs(docs, sc);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: stackoverflow.com

IndexSearcher searcher = new IndexSearcher("path_to_index");
MatchAllDocsQuery everyDocClause = new MatchAllDocsQuery();
TermQuery termClause = new TermQuery(new Term("text", "exclude_term"));
BooleanQuery query = new BooleanQuery();
query.add(everyDocClause, BooleanClause.Occur.MUST);
query.add(termClause, BooleanClause.Occur.MUST_NOT);
Hits hits = searcher.search(query);

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

@Override
public Query rewrite(IndexReader reader) throws IOException {
 final int threshold = Math.min(BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD, BooleanQuery.getMaxClauseCount());
 if (termData.size() <= threshold) {
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  TermIterator iterator = termData.iterator();
  for (BytesRef term = iterator.next(); term != null; term = iterator.next()) {
   bq.add(new TermQuery(new Term(iterator.field(), BytesRef.deepCopyOf(term))), Occur.SHOULD);
  }
  return new ConstantScoreQuery(bq.build());
 }
 return super.rewrite(reader);
}

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

protected Query notIn(Operation<?> operation, QueryMetadata metadata,
    boolean ignoreCase) {
  BooleanQuery bq = new BooleanQuery();
  bq.add(new BooleanClause(in(operation, metadata, false), Occur.MUST_NOT));
  bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
  return bq;
}

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

final int threshold = Math.min(BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD, BooleanQuery.getMaxClauseCount());
assert termData.size() > threshold : "Query should have been rewritten";
List<TermAndState> matchingTerms = new ArrayList<>(threshold);
 BooleanQuery.Builder bq = new BooleanQuery.Builder();
 for (TermAndState t : matchingTerms) {
  final TermContext termContext = new TermContext(searcher.getTopReaderContext());
  termContext.register(t.state, context.ord, t.docFreq, t.totalTermFreq);
  bq.add(new TermQuery(new Term(t.field, t.term), termContext), Occur.SHOULD);
 Query q = new ConstantScoreQuery(bq.build());
 final Weight weight = searcher.rewrite(q).createWeight(searcher, needsScores, score());
 return new WeightOrDocIdSet(weight);
} else {

代码示例来源:origin: stackoverflow.com

TermQuery tq= new TermQuery(new Term("url", url));
// BooleanClauses Enum SHOULD says Use this operator for clauses that should appear in the matching documents.
BooleanQuery bq = new BooleanQuery().add(tq,BooleanClause.Occur.SHOULD);
IndexSearcher searcher = new IndexSearcher(index, true);
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
searcher.search(query, collector);

代码示例来源: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: linkedin/indextank-engine

public org.apache.lucene.search.Query getLuceneQuery() {
  BooleanQuery bq = new BooleanQuery();
  if (leftQuery instanceof OrQuery) {
    for (BooleanClause bc : ((BooleanQuery)leftQuery.getLuceneQuery()).getClauses()) {
      bq.add(bc);
    }
  } else {
    bq.add(leftQuery.getLuceneQuery(), BooleanClause.Occur.SHOULD);
  }
  if (rightQuery instanceof OrQuery) {
    for (BooleanClause bc : ((BooleanQuery)rightQuery.getLuceneQuery()).getClauses()) {
      bq.add(bc);
    }            
  } else {
    bq.add(rightQuery.getLuceneQuery(), BooleanClause.Occur.SHOULD);
  }
  return bq;
}

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

@Override
protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
  BooleanQuery.Builder highBuilder = new BooleanQuery.Builder();
  BooleanQuery.Builder lowBuilder = new BooleanQuery.Builder();
  for (int i = 0; i < terms.length; i++) {
    Query query = new TermQuery(terms[i], ctx[i]);
    if (boosts != null && boosts[i] != 1f) {
      query = new BoostQuery(query, boosts[i]);
        || (docFreqs[i] > (int) Math.ceil(maxTermFrequency
      highBuilder.add(query, BooleanClause.Occur.SHOULD);
    } else {
      lowBuilder.add(query, BooleanClause.Occur.SHOULD);
  BooleanQuery high = highBuilder.build();
  BooleanQuery low = lowBuilder.build();
  if (low.clauses().isEmpty()) {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (BooleanClause booleanClause : high) {
      queryBuilder.add(booleanClause.getQuery(), Occur.MUST);
    return queryBuilder.build();
  } else if (high.clauses().isEmpty()) {
    return low;
  } else {

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

return false;
} else if (query instanceof TermQuery) {
  return mightMatchNonNestedDocs(((TermQuery) query).getTerm().field(), nestedPath);
} else if (query instanceof TermInSetQuery) {
  PrefixCodedTerms terms = ((TermInSetQuery) query).getTermData();
} else if (query instanceof BooleanQuery) {
  final BooleanQuery bq = (BooleanQuery) query;
  final boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired);
  if (hasRequiredClauses) {
    return bq.clauses().stream()
        .filter(BooleanClause::isRequired)
        .map(BooleanClause::getQuery)
        .allMatch(q -> mightMatchNonNestedDocs(q, nestedPath));
  } else {
    return bq.clauses().stream()
        .filter(c -> c.getOccur() == Occur.SHOULD)
        .map(BooleanClause::getQuery)
        .anyMatch(q -> mightMatchNonNestedDocs(q, nestedPath));

代码示例来源:origin: ch.epfl.bbp.nlp/bluima_text2pmid

public Txt2PubmedId(File indexPath) throws IOException {
  Directory dir = FSDirectory.open(indexPath);
  indexReader = DirectoryReader.open(dir);
  searcher = new IndexSearcher(indexReader);
  Analyzer analyzer = Txt2PubmedIdIndexer.getAnalyzer();
  parser = new QueryParser(LUCENE_41, CONTENT_FIELD, analyzer);
  BooleanQuery.setMaxClauseCount(MAX_VALUE);
}

代码示例来源: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;
}

相关文章