Java Code Examples for org.apache.lucene.search.BooleanClause.Occur

The following examples show how to use org.apache.lucene.search.BooleanClause.Occur. These examples are extracted from open source projects. 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 Project: onedev   Source File: RegexLiterals.java    License: MIT License 6 votes vote down vote up
/**
 * @param fieldName
 * @param gramSize
 * @return
 * @throws TooGeneralQueryException
 */
public Query asNGramQuery(String fieldName, int gramSize) throws TooGeneralQueryException {
	BooleanQuery.Builder orQueryBuilder = new BooleanQuery.Builder();
	for (List<LeafLiterals> row: rows) {
		BooleanQuery.Builder andQueryBuilder = new BooleanQuery.Builder();
		for (LeafLiterals literals: row) {
			if (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE)
				andQueryBuilder.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST);
		}
		BooleanQuery andQuery = andQueryBuilder.build();
		if (andQuery.clauses().size() != 0)
			orQueryBuilder.add(andQuery, Occur.SHOULD);
	}
	BooleanQuery orQuery = orQueryBuilder.build();
	if (orQuery.clauses().size() != 0)
		return orQuery;
	else
		throw new TooGeneralQueryException();
}
 
Example 2
Source Project: jstarcraft-core   Source File: LuceneAccessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryIntersection(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example 3
Source Project: jstarcraft-core   Source File: LuceneAccessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryUnion(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example 4
Source Project: webdsl   Source File: AbstractEntitySearcher.java    License: Apache License 2.0 6 votes vote down vote up
private Query createRegexQuery ( QueryDef qd ) {
    BooleanQuery query = new BooleanQuery();
    List<SpanQuery> spanClausesList = new ArrayList<SpanQuery>();
    String[] queryStrings;
    SpanQuery[] spanClausesArray;
    RegexQuery regexQuery;
    for ( String fld : qd.fields ) {
        spanClausesList.clear();
        queryStrings = qd.query.split(" ");
        spanClausesArray = new SpanQuery[queryStrings.length];
        for ( String subquery : queryStrings ) {
            regexQuery = new RegexQuery( new Term( fld, subquery ) );
            regexQuery.setRegexImplementation( new JavaUtilRegexCapabilities() );
            //if emptyable, like a query '(optional)?' or 'bla|a*', make span optional by wrapping it SpanOrQuery
            if(Pattern.matches(subquery, "")){
                spanClausesList.add( new SpanOrQuery( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) ) );
            } else {
                spanClausesList.add( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) );
            }
        }

        spanClausesList.toArray( spanClausesArray );
        query.add( new SpanNearQuery( spanClausesArray, 0, true), Occur.SHOULD );
    }
    return query;
}
 
Example 5
Source Project: jstarcraft-core   Source File: LuceneAccessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterateUnion(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	engine.iterateDocuments((document) -> {
		iterator.iterate((T) metadata.decodeDocument(document));
	}, query, null, offset, size);
}
 
Example 6
Source Project: netbeans   Source File: DocumentUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static BooleanQuery createFQNQuery(final String resourceName) {
    String pkgName;
    String sName;
    int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR);
    if (index < 0) {
        pkgName = "";       //NOI18N
        sName = resourceName;
    } else {
        pkgName = resourceName.substring(0, index);
        sName = resourceName.substring(index+1);
    }
    final BooleanQuery snQuery = new BooleanQuery();
    snQuery.add (new WildcardQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + DocumentUtil.WILDCARD_QUERY_WILDCARD)),Occur.SHOULD);
    snQuery.add (new PrefixQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + '$')),Occur.SHOULD);   //NOI18N
    if (pkgName.length() == 0) {
        return snQuery;
    }
    final BooleanQuery fqnQuery = new BooleanQuery();
    fqnQuery.add(new TermQuery(new Term(DocumentUtil.FIELD_PACKAGE_NAME,pkgName)), Occur.MUST);
    fqnQuery.add(snQuery, Occur.MUST);
    return fqnQuery;
}
 
Example 7
@Override
public void addQueryTerms(BooleanQuery query, AdvancedSearchParams params) {
    try {
        ZipscriptQueryParams queryParams = params.getExtensionData(ZipscriptQueryParams.ZIPSCRIPTQUERYPARAMS);
        if (queryParams.getMinPresent() != null || queryParams.getMaxPresent() != null) {
            Query presentQuery = NumericRangeQuery.newIntRange("present",
                    queryParams.getMinPresent(), queryParams.getMaxPresent(), true, true);
            query.add(presentQuery, Occur.MUST);
        }
        if (queryParams.getMinMissing() != null || queryParams.getMaxMissing() != null) {
            Query missingQuery = NumericRangeQuery.newIntRange("missing",
                    queryParams.getMinMissing(), queryParams.getMaxMissing(), true, true);
            query.add(missingQuery, Occur.MUST);
        }
        if (queryParams.getMinPercent() != null || queryParams.getMaxPercent() != null) {
            Query percentQuery = NumericRangeQuery.newIntRange("percent",
                    queryParams.getMinPercent(), queryParams.getMaxPercent(), true, true);
            query.add(percentQuery, Occur.MUST);
        }
    } catch (KeyNotFoundException e) {
        // No MP3 terms to include, return without amending query
    }
}
 
Example 8
Source Project: development   Source File: LuceneQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private static BooleanQuery constructDefaultLocaleHandlingQuery(
        String fieldName, String locale, String defaultLocale,
        String searchPhrase) {
    BooleanQuery bq1 = new BooleanQuery();
    TermQuery tq1 = new TermQuery(
            new Term(fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX,
                    defaultLocale));
    TermQuery tq2 = new TermQuery(new Term(
            fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, locale));
    bq1.add(tq1, Occur.MUST);
    bq1.add(tq2, Occur.MUST_NOT);
    BooleanQuery bq2 = new BooleanQuery();
    WildcardQuery wq1 = new WildcardQuery(
            new Term(fieldName + defaultLocale,
                    "*" + searchPhrase.toLowerCase() + "*"));
    bq2.add(wq1, Occur.SHOULD);
    BooleanQuery finalQuery = new BooleanQuery();
    finalQuery.add(bq1, Occur.MUST);
    finalQuery.add(bq2, Occur.MUST);

    return finalQuery;
}
 
Example 9
Source Project: lucene-solr   Source File: TestMultiFieldQPHelper.java    License: Apache License 2.0 6 votes vote down vote up
public void testStaticMethod2Old() throws QueryNodeException {
  String[] fields = { "b", "t" };
  BooleanClause.Occur[] flags = { BooleanClause.Occur.MUST,
      BooleanClause.Occur.MUST_NOT };
  StandardQueryParser parser = new StandardQueryParser();
  parser.setMultiFields(fields);
  parser.setAnalyzer(new MockAnalyzer(random()));

  Query q = QueryParserUtil.parse("one", fields, flags,
      new MockAnalyzer(random()));// , fields, flags, new
  // MockAnalyzer());
  assertEquals("+b:one -t:one", q.toString());

  q = QueryParserUtil.parse("one two", fields, flags, new MockAnalyzer(random()));
  assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

  // expected exception, array length differs
  expectThrows(IllegalArgumentException.class, () -> {
    BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
    QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random()));
  });
}
 
Example 10
Source Project: Elasticsearch   Source File: QueryCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    final Query query = getQuery(doc);
    if (query == null) {
        // log???
        return;
    }
    Query existsQuery = query;
    if (isNestedDoc) {
        existsQuery = new BooleanQuery.Builder()
            .add(existsQuery, Occur.MUST)
            .add(Queries.newNonNestedFilter(), Occur.FILTER)
            .build();
    }
    // run the query
    try {
        if (Lucene.exists(searcher, existsQuery)) {
            topDocsLeafCollector.collect(doc);
            postMatch(doc);
        }
    } catch (IOException e) {
        logger.warn("[" + current.utf8ToString() + "] failed to execute query", e);
    }
}
 
Example 11
Source Project: Elasticsearch   Source File: PercolatorService.java    License: Apache License 2.0 6 votes vote down vote up
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException {
    Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter();

    final Query filter;
    if (context.aliasFilter() != null) {
        BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder();
        booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST);
        booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST);
        filter = booleanFilter.build();
    } else {
        filter = percolatorTypeFilter;
    }

    Query query = Queries.filtered(context.percolateQuery(), filter);
    percolatorSearcher.searcher().search(query, percolateCollector);
    percolateCollector.aggregatorCollector.postCollection();
    if (context.aggregations() != null) {
        aggregationPhase.execute(context);
    }
}
 
Example 12
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
private Query randomBooleanQuery() {
  if (random().nextInt(10) == 0) {
    return new BoostQuery(randomBooleanQuery(), TestUtil.nextInt(random(), 1, 10));
  }
  final int numClauses = random().nextInt(5);
  BooleanQuery.Builder b = new BooleanQuery.Builder();
  int numShoulds = 0;
  for (int i = 0; i < numClauses; ++i) {
    final Occur occur = Occur.values()[random().nextInt(Occur.values().length)];
    if (occur == Occur.SHOULD) {
      numShoulds++;
    }
    final Query query = randomQuery();
    b.add(query, occur);
  }
  b.setMinimumNumberShouldMatch(random().nextBoolean() ? 0 : TestUtil.nextInt(random(), 0, numShoulds + 1));
  return b.build();
}
 
Example 13
Source Project: lucene-solr   Source File: TestMultiFieldQueryParser.java    License: Apache License 2.0 6 votes vote down vote up
public void testStaticMethod2Old() throws ParseException {
  String[] fields = {"b", "t"};
  //int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};

  Query q = MultiFieldQueryParser.parse("one", fields, flags, new MockAnalyzer(random()));//, fields, flags, new MockAnalyzer(random));
  assertEquals("+b:one -t:one", q.toString());

  q = MultiFieldQueryParser.parse("one two", fields, flags, new MockAnalyzer(random()));
  assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

  // expected exception, array length differs
  expectThrows(IllegalArgumentException.class, () -> {
    BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
    MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random()));
  });
}
 
Example 14
Source Project: development   Source File: SearchServiceBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the Lucene query for the given locale and query text.
 * 
 * @param searchString
 *            the text query for the Lucene query parser
 * @param mId
 *            the marketplace id
 * @param locale
 *            the locale for the analyzer to use
 * @param isDefaultLocaleHandling
 * @return the Lucene query for the given locale and query text
 * @throws ParseException
 *             in case the query cannot be parsed
 */
private org.apache.lucene.search.Query getLuceneQuery(String searchString,
        String mId, String locale,
        boolean isDefaultLocaleHandling) throws SyntaxError, QueryNodeException {

    // construct wildcard query for the actual search part
    org.apache.lucene.search.Query textQuery = LuceneQueryBuilder
            .getServiceQuery(searchString, locale, DEFAULT_LOCALE,
                    isDefaultLocaleHandling);

    // build mId part
    TermQuery mIdQuery = new TermQuery(new Term(ProductClassBridge.MP_ID,
            QueryParser.escape(mId).toLowerCase()));

    // now construct final query
    BooleanQuery query = new BooleanQuery();
    query.add(mIdQuery, Occur.MUST);
    query.add(textQuery, Occur.MUST);
    return query;
}
 
Example 15
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testDeduplicateMustClauses() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  Query query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .build();
  Query expected = new BoostQuery(new TermQuery(new Term("foo", "bar")), 2);
  assertEquals(expected, searcher.rewrite(query));

  query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 2), Occur.MUST)
      .add(new TermQuery(new Term("foo", "quux")), Occur.MUST)
      .build();
  expected = new BooleanQuery.Builder()
      .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 3), Occur.MUST)
      .add(new TermQuery(new Term("foo", "quux")), Occur.MUST)
      .build();
  assertEquals(expected, searcher.rewrite(query));
}
 
Example 16
Source Project: lucene-solr   Source File: FieldTermStackTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test2Terms() throws Exception {
  makeIndex();
  
  BooleanQuery.Builder query = new BooleanQuery.Builder();
  query.add( tq( "b" ), Occur.SHOULD );
  query.add( tq( "c" ), Occur.SHOULD );
  FieldQuery fq = new FieldQuery( query.build(), true, true );
  FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
  assertEquals( 8, stack.termList.size() );
  assertEquals( "b(6,7,3)", stack.pop().toString() );
  assertEquals( "b(8,9,4)", stack.pop().toString() );
  assertEquals( "c(10,11,5)", stack.pop().toString() );
  assertEquals( "b(14,15,7)", stack.pop().toString() );
  assertEquals( "b(16,17,8)", stack.pop().toString() );
  assertEquals( "c(18,19,9)", stack.pop().toString() );
  assertEquals( "b(26,27,13)", stack.pop().toString() );
  assertEquals( "b(30,31,15)", stack.pop().toString() );
}
 
Example 17
Source Project: lucene-solr   Source File: FieldQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetTermSet() throws Exception {
  BooleanQuery.Builder query = new BooleanQuery.Builder();
  query.add(new TermQuery(new Term(F, "A")), Occur.MUST);
  query.add(new TermQuery(new Term(F, "B")), Occur.MUST);
  query.add(new TermQuery(new Term("x", "C")), Occur.SHOULD);

  BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
  innerQuery.add(new TermQuery(new Term(F, "D")), Occur.MUST);
  innerQuery.add(new TermQuery(new Term(F, "E")), Occur.MUST);
  query.add(innerQuery.build(), Occur.MUST_NOT);

  FieldQuery fq = new FieldQuery( query.build(), true, true );
  assertEquals( 2, fq.termSetMap.size() );
  Set<String> termSet = fq.getTermSet( F );
  assertEquals( 2, termSet.size() );
  assertTrue( termSet.contains( "A" ) );
  assertTrue( termSet.contains( "B" ) );
  termSet = fq.getTermSet( "x" );
  assertEquals( 1, termSet.size() );
  assertTrue( termSet.contains( "C" ) );
  termSet = fq.getTermSet( "y" );
  assertNull( termSet );
}
 
Example 18
Source Project: lucene-solr   Source File: TestSimpleQueryParser.java    License: Apache License 2.0 6 votes vote down vote up
public void testComplex03() throws Exception {
  BooleanQuery.Builder expected = new BooleanQuery.Builder();
  BooleanQuery.Builder inner = new BooleanQuery.Builder();
  BooleanQuery.Builder inner2 = new BooleanQuery.Builder();
  inner2.add(new TermQuery(new Term("field", "star")), Occur.MUST);
  inner2.add(new TermQuery(new Term("field", "wars")), Occur.MUST);
  inner.add(inner2.build(), Occur.SHOULD);
  inner.add(new TermQuery(new Term("field", "empire")), Occur.SHOULD);
  inner.add(new TermQuery(new Term("field", "strikes")), Occur.SHOULD);
  expected.add(inner.build(), Occur.MUST);
  expected.add(new TermQuery(new Term("field", "back")), Occur.MUST);

  assertEquals(expected.build(), parse("star wars | empire | strikes back"));
  assertEquals(expected.build(), parse("star + wars|empire | strikes + back"));
  assertEquals(expected.build(), parse("star + | wars | ----empire | + --strikes + | --back \\"));
}
 
Example 19
Source Project: lucene-solr   Source File: TestBoolean2ScorerSupplier.java    License: Apache License 2.0 6 votes vote down vote up
public void testProhibitedLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  // The MUST_NOT clause is called with the same lead cost as the MUST clause
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(80, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect
}
 
Example 20
Source Project: lucene-solr   Source File: QueryUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Combines a scoring query with a non-scoring (filter) query.
 * If both parameters are null then return a {@link MatchAllDocsQuery}.
 * If only {@code scoreQuery} is present then return it.
 * If only {@code filterQuery} is present then return it wrapped with constant scoring.
 * If neither are null then we combine with a BooleanQuery.
 */
public static Query combineQueryAndFilter(Query scoreQuery, Query filterQuery) {
  // check for *:* is simple and avoids needless BooleanQuery wrapper even though BQ.rewrite optimizes this away
  if (scoreQuery == null || scoreQuery instanceof MatchAllDocsQuery) {
    if (filterQuery == null) {
      return new MatchAllDocsQuery(); // default if nothing -- match everything
    } else {
      return new ConstantScoreQuery(filterQuery);
    }
  } else {
    if (filterQuery == null || filterQuery instanceof MatchAllDocsQuery) {
      return scoreQuery;
    } else {
      return new BooleanQuery.Builder()
          .add(scoreQuery, Occur.MUST)
          .add(filterQuery, Occur.FILTER)
          .build();
    }
  }
}
 
Example 21
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testRemoveMatchAllFilter() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  BooleanQuery bq = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new MatchAllDocsQuery(), Occur.FILTER)
      .build();
  assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq));

  bq = new BooleanQuery.Builder()
      .setMinimumNumberShouldMatch(random().nextInt(5))
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
      .add(new MatchAllDocsQuery(), Occur.FILTER)
      .build();
  BooleanQuery expected = new BooleanQuery.Builder()
      .setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch())
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
      .build();
  assertEquals(expected, searcher.rewrite(bq));
}
 
Example 22
Source Project: incubator-retired-blur   Source File: SuperParser.java    License: Apache License 2.0 6 votes vote down vote up
private Query fixNegatives(Query query) {
  if (query instanceof SuperQuery) {
    SuperQuery superQuery = (SuperQuery) query;
    fixNegatives(superQuery.getQuery());
  } else if (query instanceof BooleanQuery) {
    BooleanQuery booleanQuery = (BooleanQuery) query;
    for (BooleanClause clause : booleanQuery.clauses()) {
      fixNegatives(clause.getQuery());
    }
    if (containsAllNegativeQueries(booleanQuery)) {
      if (containsSuperQueries(booleanQuery)) {
        booleanQuery.add(new TermQuery(_defaultPrimeDocTerm), Occur.SHOULD);
      } else {
        booleanQuery.add(new MatchAllDocsQuery(), Occur.SHOULD);
      }
    }
  }
  return query;
}
 
Example 23
private List<ArtifactBean> getValidArtifacts(List<ArtifactBean> artifactList) throws ServiceException {
	if (artifactList.isEmpty()) {
		return artifactList;
	}
	
	List<ArtifactBean> validArtifacts = Lists.newArrayList();
	List<List<ArtifactBean>> artifactListPartitions = Lists.partition(artifactList, MAX_CLAUSES);
	
	for (List<ArtifactBean> partialArtifactList : artifactListPartitions) {
		BooleanQuery artifactSearchQuery = new BooleanQuery();
		
		for (ArtifactBean artifactBean : partialArtifactList) {
			if (isValidId(artifactBean.getGroupId()) && isValidId(artifactBean.getArtifactId())) {
				BooleanQuery bq = new BooleanQuery();
				bq.add(new TermQuery(new Term(MavenCentralSearchApiConstants.GROUP_FIELD, artifactBean.getGroupId())), Occur.MUST);
				bq.add(new TermQuery(new Term(MavenCentralSearchApiConstants.ARTIFACT_FIELD, artifactBean.getArtifactId())), Occur.MUST);
				
				if (StringUtils.hasText(artifactBean.getType())) {
					bq.add(new TermQuery(new Term(MavenCentralSearchApiConstants.TYPE_FIELD, artifactBean.getType())), Occur.MUST);
				}
				artifactSearchQuery.add(bq, Occur.SHOULD);
			}
		}
		
		if (artifactSearchQuery.clauses().size() > 0) {
			SolrQuery solrQuery = new SolrQuery(LuceneUtils.queryToString(artifactSearchQuery));
			QueryResponse response = query(solrQuery, 0, partialArtifactList.size());
			
			validArtifacts.addAll(response.getBeans(ArtifactBean.class));
		}
	}
	
	Collections.sort(validArtifacts);
	return validArtifacts;
}
 
Example 24
Source Project: nifi   Source File: LuceneUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static org.apache.lucene.search.Query convertQuery(final org.apache.nifi.provenance.search.Query query) {
    if (query.getStartDate() == null && query.getEndDate() == null && query.getSearchTerms().isEmpty()) {
        return new MatchAllDocsQuery();
    }

    final BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (final SearchTerm searchTerm : query.getSearchTerms()) {
        final String searchValue = searchTerm.getValue();
        if (searchValue == null) {
            throw new IllegalArgumentException("Empty search value not allowed (for term '" + searchTerm.getSearchableField().getFriendlyName() + "')");
        }

        if (searchValue.contains("*") || searchValue.contains("?")) {
            queryBuilder.add(new BooleanClause(new WildcardQuery(new Term(searchTerm.getSearchableField().getSearchableFieldName(), searchTerm.getValue().toLowerCase())), Occur.MUST));
        } else {
            queryBuilder.add(new BooleanClause(new TermQuery(new Term(searchTerm.getSearchableField().getSearchableFieldName(), searchTerm.getValue().toLowerCase())), Occur.MUST));
        }
    }

    if (query.getMinFileSize() != null || query.getMaxFileSize() != null) {
        final long minBytes = query.getMinFileSize() == null ? 0L : DataUnit.parseDataSize(query.getMinFileSize(), DataUnit.B).longValue();
        final long maxBytes = query.getMaxFileSize() == null ? Long.MAX_VALUE : DataUnit.parseDataSize(query.getMaxFileSize(), DataUnit.B).longValue();
        queryBuilder.add(LongPoint.newRangeQuery(SearchableFields.FileSize.getSearchableFieldName(), minBytes, maxBytes), Occur.MUST);
    }

    if (query.getStartDate() != null || query.getEndDate() != null) {
        final long minDateTime = query.getStartDate() == null ? 0L : query.getStartDate().getTime();
        final long maxDateTime = query.getEndDate() == null ? Long.MAX_VALUE : query.getEndDate().getTime();
        queryBuilder.add(LongPoint.newRangeQuery(SearchableFields.EventTime.getSearchableFieldName(), minDateTime, maxDateTime), Occur.MUST);
    }

    return queryBuilder.build();
}
 
Example 25
Source Project: lucene-solr   Source File: CommonTermsQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visit(QueryVisitor visitor) {
  Term[] selectedTerms = terms.stream().filter(t -> visitor.acceptField(t.field())).toArray(Term[]::new);
  if (selectedTerms.length > 0) {
    QueryVisitor v = visitor.getSubVisitor(Occur.SHOULD, this);
    v.consumeTerms(this, selectedTerms);
  }
}
 
Example 26
@Override
public void warm(IndexSearcher searcher) throws IOException {
  // TODO (Yuri Z): Run some diverse searches, searching against all
  // fields.

  BooleanQuery participantQuery = new BooleanQuery();
  participantQuery.add(
      new TermQuery(new Term(WITH.toString(), sharedDomainParticipantId.getAddress())),
      Occur.SHOULD);
  searcher.search(participantQuery, MAX_WAVES);
}
 
Example 27
Source Project: lucene-solr   Source File: TestMultiFieldQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void testStaticMethod3Old() throws ParseException {
  String[] queries = {"one", "two"};
  String[] fields = {"b", "t"};
    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
  Query q = MultiFieldQueryParser.parse(queries, fields, flags, new MockAnalyzer(random()));
  assertEquals("+b:one -t:two", q.toString());

  // expected exception, array length differs
  expectThrows(IllegalArgumentException.class, () -> {
    BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
    MultiFieldQueryParser.parse(queries, fields, flags2, new MockAnalyzer(random()));
  });
}
 
Example 28
Source Project: lucene-solr   Source File: BooleanWeight.java    License: Apache License 2.0 5 votes vote down vote up
BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException {
  List<BulkScorer> optional = new ArrayList<BulkScorer>();
  for (WeightedBooleanClause wc : weightedClauses) {
    Weight w = wc.weight;
    BooleanClause c = wc.clause;
    if (c.getOccur() != Occur.SHOULD) {
      continue;
    }
    BulkScorer subScorer = w.bulkScorer(context);

    if (subScorer != null) {
      optional.add(subScorer);
    }
  }

  if (optional.size() == 0) {
    return null;
  }

  if (query.getMinimumNumberShouldMatch() > optional.size()) {
    return null;
  }

  if (optional.size() == 1) {
    return optional.get(0);
  }

  return new BooleanScorer(this, optional, Math.max(1, query.getMinimumNumberShouldMatch()), scoreMode.needsScores());
}
 
Example 29
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 5 votes vote down vote up
public void testDeduplicateShouldClauses() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  Query query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)
      .build();
  Query expected = new BoostQuery(new TermQuery(new Term("foo", "bar")), 2);
  assertEquals(expected, searcher.rewrite(query));

  query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)
      .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 2), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "quux")), Occur.SHOULD)
      .build();
  expected = new BooleanQuery.Builder()
      .add(new BoostQuery(new TermQuery(new Term("foo", "bar")), 3), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "quux")), Occur.SHOULD)
      .build();
  assertEquals(expected, searcher.rewrite(query));

  query = new BooleanQuery.Builder()
      .setMinimumNumberShouldMatch(2)
      .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "quux")), Occur.SHOULD)
      .build();
  expected = query;
  assertEquals(expected, searcher.rewrite(query));
}
 
Example 30
Source Project: lucene-solr   Source File: TestSimpleQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
/** test negated term */
public void testNOT() throws Exception {
  BooleanQuery.Builder expected = new BooleanQuery.Builder();
  expected.add(new TermQuery(new Term("field", "foo")), Occur.MUST_NOT);
  expected.add(new MatchAllDocsQuery(), Occur.SHOULD);

  assertEquals(expected.build(), parse("-foo"));
  assertEquals(expected.build(), parse("-(foo)"));
  assertEquals(expected.build(), parse("---foo"));
}