org.apache.lucene.search.BooleanQuery.Builder Java Examples

The following examples show how to use org.apache.lucene.search.BooleanQuery.Builder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VocabularyNeo4jImpl.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
void addCommonConstraints(Builder indexQuery, Query query) {
  // BooleanQuery categoryQueries = new BooleanQuery();
  Builder categoryQueriesBuilder = new BooleanQuery.Builder();
  for (String category : query.getCategories()) {
    categoryQueriesBuilder.add(new TermQuery(new Term(Concept.CATEGORY, category)), Occur.SHOULD);
  }
  if (!query.getCategories().isEmpty()) {
    indexQuery.add(new BooleanClause(categoryQueriesBuilder.build(), Occur.MUST));
  }

  // BooleanQuery prefixQueries = new BooleanQuery();
  Builder prefixQueriesBuilder = new BooleanQuery.Builder();
  for (String curie : query.getPrefixes()) {
    String prefix = curieUtil.getExpansion(curie);
    prefixQueriesBuilder.add(new WildcardQuery(new Term(CommonProperties.IRI, prefix + "*")),
        Occur.SHOULD);
  }
  if (!query.getPrefixes().isEmpty()) {
    indexQuery.add(new BooleanClause(prefixQueriesBuilder.build(), Occur.MUST));
  }
}
 
Example #2
Source File: SolrAuthoritySetQuery.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BitsFilter getOwnerFilter(String[] auths, SolrIndexSearcher searcher) throws IOException
{
    Builder builder = new BooleanQuery.Builder();
    for(String current : auths)
    {
        if (AuthorityType.getAuthorityType(current) == AuthorityType.USER)
        {
        	builder.add(new TermQuery(new Term(QueryConstants.FIELD_OWNER, current)), BooleanClause.Occur.SHOULD);
        }
    }

    BitsFilterCollector collector = new BitsFilterCollector(searcher.getTopReaderContext().leaves().size());
    searcher.search(builder.build(), collector);
    return collector.getBitsFilter();
}
 
Example #3
Source File: VocabularyNeo4jImpl.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public List<Concept> searchConcepts(Query query) {
  QueryParser parser = getQueryParser();
  // BooleanQuery finalQuery = new BooleanQuery();
  Builder finalQueryBuilder = new BooleanQuery.Builder();
  try {
    if (query.isIncludeSynonyms() || query.isIncludeAbbreviations() || query.isIncludeAcronyms()) {
      // BooleanQuery subQuery = new BooleanQuery();
      Builder subQueryBuilder = new BooleanQuery.Builder();
      subQueryBuilder.add(LuceneUtils.getBoostedQuery(parser, query.getInput(), 10.0f),
          Occur.SHOULD);
      String escapedQuery = QueryParser.escape(query.getInput());
      if (query.isIncludeSynonyms()) {
        subQueryBuilder.add(parser.parse(Concept.SYNONYM + ":" + escapedQuery), Occur.SHOULD);
      }
      if (query.isIncludeAbbreviations()) {
        subQueryBuilder.add(parser.parse(Concept.ABREVIATION + ":" + escapedQuery), Occur.SHOULD);
      }
      if (query.isIncludeAcronyms()) {
        subQueryBuilder.add(parser.parse(Concept.ACRONYM + ":" + escapedQuery), Occur.SHOULD);
      }
      finalQueryBuilder.add(subQueryBuilder.build(), Occur.MUST);
    } else {
      finalQueryBuilder.add(parser.parse(query.getInput()), Occur.MUST);
    }
  } catch (ParseException e) {
    logger.log(Level.WARNING, "Failed to parse query", e);
  }
  addCommonConstraints(finalQueryBuilder, query);
  IndexHits<Node> hits = null;
  BooleanQuery finalQuery = finalQueryBuilder.build();

  try (Transaction tx = graph.beginTx()) {
    hits = graph.index().getNodeAutoIndexer().getAutoIndex().query(finalQuery);
    tx.success();
    return limitHits(hits, query);
  }
}
 
Example #4
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected BooleanQuery.Builder getTopLevelBuilder() {
  return new BooleanQuery.Builder();
}
 
Example #5
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected Query build(Builder builder) {
  return builder.build();
}
 
Example #6
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docCount, float boost, TermStates states) {
  final TermQuery tq = new TermQuery(term, states);
  topLevel.add(new BoostQuery(tq, boost), BooleanClause.Occur.SHOULD);
}
 
Example #7
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected BlendedTermQuery.Builder getTopLevelBuilder() {
  BlendedTermQuery.Builder builder = new BlendedTermQuery.Builder();
  builder.setRewriteMethod(BlendedTermQuery.BOOLEAN_REWRITE);
  return builder;
}
 
Example #8
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected Query build(BlendedTermQuery.Builder builder) {
  return builder.build();
}
 
Example #9
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void addClause(BlendedTermQuery.Builder topLevel, Term term, int docCount,
    float boost, TermStates states) {
  topLevel.add(term, boost, states);
}
 
Example #10
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected BooleanQuery.Builder getTopLevelBuilder() {
  return new BooleanQuery.Builder();
}
 
Example #11
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected Query build(BooleanQuery.Builder builder) {
  return builder.build();
}
 
Example #12
Source File: MultiTermQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void addClause(BooleanQuery.Builder topLevel, Term term, int docFreq, float boost, TermStates states) {
  final Query q = new ConstantScoreQuery(new TermQuery(term, states));
  topLevel.add(new BoostQuery(q, boost), BooleanClause.Occur.SHOULD);
}
 
Example #13
Source File: VocabularyNeo4jImpl.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Override
public List<Concept> getConceptsFromPrefix(Query query) {
  QueryParser parser = getQueryParser();
  // BooleanQuery finalQuery = new BooleanQuery();
  Builder finalQueryBuilder = new BooleanQuery.Builder();
  try {
    // BooleanQuery subQuery = new BooleanQuery();
    Builder subQueryBuilder = new BooleanQuery.Builder();
    subQueryBuilder.add(parser.parse(formatQuery("%s%s:%s*", NodeProperties.LABEL,
        LuceneUtils.EXACT_SUFFIX, query.getInput())), Occur.SHOULD);
    Optional<String> fullUri = curieUtil.getIri(query.getInput());
    if (fullUri.isPresent()) {
      subQueryBuilder.add(
          parser.parse(formatQuery("%s:%s*", NodeProperties.IRI, (fullUri.get()))), Occur.SHOULD);
    }

    if (query.isIncludeSynonyms()) {
      subQueryBuilder.add(
          parser.parse(formatQuery("%s%s:%s*", Concept.SYNONYM, LuceneUtils.EXACT_SUFFIX,
              query.getInput())), Occur.SHOULD);
    }
    if (query.isIncludeAbbreviations()) {
      subQueryBuilder.add(parser.parse(formatQuery("%s%s:%s*", Concept.ABREVIATION,
          LuceneUtils.EXACT_SUFFIX, query.getInput())), Occur.SHOULD);
    }
    if (query.isIncludeAcronyms()) {
      subQueryBuilder.add(
          parser.parse(formatQuery("%s%s:%s*", Concept.ACRONYM, LuceneUtils.EXACT_SUFFIX,
              query.getInput())), Occur.SHOULD);
    }

    finalQueryBuilder.add(subQueryBuilder.build(), Occur.MUST);
  } catch (ParseException e) {
    logger.log(Level.WARNING, "Failed to parse query", e);
  }
  addCommonConstraints(finalQueryBuilder, query);
  BooleanQuery finalQuery = finalQueryBuilder.build();
  IndexHits<Node> hits = null;
  try (Transaction tx = graph.beginTx()) {
    hits = graph.index().getNodeAutoIndexer().getAutoIndex().query(finalQuery);
    tx.success();
    return limitHits(hits, query);
  }

}
 
Example #14
Source File: VocabularyNeo4jImpl.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Override
public List<Concept> getConceptsFromTerm(Query query) {
  QueryParser parser = getQueryParser();
  // String exactQuery = String.format("\"\\^ %s $\"", query.getInput());
  String exactQuery = String.format("\"\\^ %s $\"", query.getInput());
  Builder finalQueryBuilder = new BooleanQuery.Builder();
  try {
    if (query.isIncludeSynonyms() || query.isIncludeAbbreviations() || query.isIncludeAcronyms()) {
      Builder subQueryBuilder = new BooleanQuery.Builder();
      // subQuery.add(LuceneUtils.getBoostedQuery(parser, exactQuery, 10.0f), Occur.SHOULD);
      subQueryBuilder.add(LuceneUtils.getBoostedQuery(parser, exactQuery, 10.0f), Occur.SHOULD);
      if (query.isIncludeSynonyms()) {
        // subQuery.add(parser.parse(Concept.SYNONYM + ":" + exactQuery), Occur.SHOULD);
        subQueryBuilder.add(parser.parse(Concept.SYNONYM + ":" + exactQuery), Occur.SHOULD);
      }
      if (query.isIncludeAbbreviations()) {
        // subQuery.add(parser.parse(Concept.ABREVIATION + ":" + exactQuery), Occur.SHOULD);
        subQueryBuilder.add(parser.parse(Concept.ABREVIATION + ":" + exactQuery), Occur.SHOULD);
      }
      if (query.isIncludeAcronyms()) {
        // subQuery.add(parser.parse(Concept.ACRONYM + ":" + exactQuery), Occur.SHOULD);
        subQueryBuilder.add(parser.parse(Concept.ACRONYM + ":" + exactQuery), Occur.SHOULD);
      }
      // finalQuery.add(subQuery, Occur.MUST);
      finalQueryBuilder.add(subQueryBuilder.build(), Occur.MUST);
    } else {
      // finalQuery.add(parser.parse(exactQuery), Occur.MUST);
      finalQueryBuilder.add(parser.parse(exactQuery), Occur.MUST);
    }
  } catch (ParseException e) {
    logger.log(Level.WARNING, "Failed to parse query", e);
  }
  addCommonConstraints(finalQueryBuilder, query);
  BooleanQuery finalQuery = finalQueryBuilder.build();
  logger.finest(finalQuery.toString());
  try (Transaction tx = graph.beginTx()) {
    IndexHits<Node> hits = graph.index().getNodeAutoIndexer().getAutoIndex().query(finalQuery);
    tx.success();
    return limitHits(hits, query);
  }
}
 
Example #15
Source File: IndexApi.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Search where we return if any of the given strings appear.
 *
 * Accepts wildcard in queries
 */
public SearchResult searchWildcard(OrderedMap<String, Set<String>> fieldNameToValues, boolean applyAllDeletes,
        IDocumentsVisitor visitor, Map<String, String> translateFields, String... fieldsToLoad)
        throws IOException {
    Builder booleanQueryBuilder = new BooleanQuery.Builder();
    Set<Entry<String, Set<String>>> entrySet = fieldNameToValues.entrySet();
    for (Entry<String, Set<String>> entry : entrySet) {
        Builder fieldQueryBuilder = new BooleanQuery.Builder();
        String fieldName = entry.getKey();
        if (translateFields != null) {
            String newFieldName = translateFields.get(fieldName);
            if (newFieldName != null) {
                fieldName = newFieldName;
            }
        }
        boolean allNegate = true;
        for (String s : entry.getValue()) {
            if (s.length() == 0) {
                throw new RuntimeException("Unable to create term for searching empty string.");
            }
            boolean negate = false;
            if (s.startsWith("!")) {
                // Negation if dealing with paths
                if (IFields.FIELDS_NEGATED_WITH_EXCLAMATION.contains(fieldName)) {
                    s = s.substring(1);
                    negate = true;
                }
            }
            if (s.length() == 0) {
                // Only a single '!' for the negate.
                continue;
            }
            if (s.indexOf('*') != -1 || s.indexOf('?') != -1) {
                if (StringUtils.containsOnlyWildCards(s)) {
                    throw new RuntimeException("Unable to create term for searching only wildcards: " + s);
                }
                fieldQueryBuilder.add(new WildcardQuery(new Term(fieldName, s)),
                        negate ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.SHOULD);

            } else {
                fieldQueryBuilder.add(new TermQuery(new Term(fieldName, s)),
                        negate ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.SHOULD);
            }
            if (!negate) {
                allNegate = false;
            }
        }
        BooleanQuery transitiveQuery = fieldQueryBuilder.build();
        if (transitiveQuery.clauses().size() != 0) {
            if (allNegate) {
                // If all are negations, we actually have to add one which would
                // match all to remove the negations.
                fieldQueryBuilder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
            }
            booleanQueryBuilder.add(fieldQueryBuilder.build(), BooleanClause.Occur.MUST);
        }
    }
    BooleanQuery booleanQuery = booleanQueryBuilder.build();
    if (DEBUG) {
        System.out.println("Searching: " + booleanQuery);
    }
    return search(booleanQuery, applyAllDeletes, visitor, fieldsToLoad);
}