Java Code Examples for org.apache.lucene.search.BooleanQuery

The following are top voted examples for showing how to use org.apache.lucene.search.BooleanQuery. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: incubator-netbeans   File: DocumentUtil.java   View source code 6 votes vote down vote up
static Query binaryNameQuery (final String resourceName) {
    final BooleanQuery query = new BooleanQuery ();
    int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR);  // NOI18N
    String pkgName, sName;
    if (index < 0) {
        pkgName = "";   // NOI18N
        sName = resourceName;
    }
    else {
        pkgName = resourceName.substring(0,index);
        sName = resourceName.substring(index+1);
    }
    sName = sName + WILDCARD_QUERY_WILDCARD;   //Type of type element (Enum, Class, Interface, Annotation)
    query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST);
    query.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName)),BooleanClause.Occur.MUST);
    return query;
}
 
Example 2
Project: incubator-netbeans   File: DocumentUtil.java   View source code 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 3
Project: Elasticsearch   File: MultiMatchQueryBuilder.java   View source code 6 votes vote down vote up
public Query combineGrouped(List<Query> queries) {
    if (queries == null || queries.isEmpty()) {
        return null;
    }
    if (queries.size() == 1) {
        return queries.get(0);
    }
    if (groupDismax) {
        return new DisjunctionMaxQuery(queries, tieBreaker);
    } else {
        BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
        for (Query query : queries) {
            booleanQuery.add(query, BooleanClause.Occur.SHOULD);
        }
        return booleanQuery.build();
    }
}
 
Example 4
Project: oscm   File: LuceneQueryBuilder.java   View source code 6 votes vote down vote up
private static BooleanQuery prepareWildcardQueryForSingleToken(String token,
        List<String> fieldNames, String locale, String defaultLocale,
        boolean isDefaultLocaleHandling) {

    BooleanQuery queryPart = new BooleanQuery();

    for (String fieldName : fieldNames) {
        if (isDefaultLocaleHandling) {
            if (locale.equals(defaultLocale)) {
                throw new IllegalArgumentException(
                        "For default locale handling, locale and default locale must be different");
            }
            BooleanQuery localeHandlingQuery = constructDefaultLocaleHandlingQuery(
                    fieldName, locale, defaultLocale, token);
            queryPart.add(localeHandlingQuery, Occur.SHOULD);
        } else {
            WildcardQuery wildcardQuery = new WildcardQuery(new Term(
                    fieldName + locale, "*" + token.toLowerCase() + "*"));
            queryPart.add(wildcardQuery, Occur.SHOULD);
        }

    }
    return queryPart;
}
 
Example 5
Project: InComb   File: IndexSearch.java   View source code 6 votes vote down vote up
/**
 * Returns lucene's document id for the given id in the given {@link IIndexTypeConf}
 * @param typeConf the 188.166.43.201 to fins.
 * @param id the id to find.
 * @return the id or 0 if document was not found.
 */
public int getDocIdForId(final IIndexTypeConf typeConf, final String id) {
	final SearchOptions params = new SearchOptions();
	params.setMaxResults(1);

	final BooleanQuery query = new BooleanQuery();
	QueryUtil.addTypeConf(query, typeConf);
	QueryUtil.addId(query, id);

	final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, params);

	if(topDocs.totalHits == 0) {
		throw new IllegalStateException("Can't find news with id " + id + " in news index.");
	}
	else if(topDocs.totalHits > 1) {
		LOGGER.warn("Found more than one result for news with id " + id + " in news index. "
				+ "This is an invalid state. Using the first found document.");
	}

	return topDocs.scoreDocs[0].doc;
}
 
Example 6
Project: InComb   File: QueryUtil.java   View source code 6 votes vote down vote up
/**
 * Adds a filter for the given search text to the given {@link BooleanQuery}.
 * If the search text is blank, than the query will not modified.
 * For each part of the search text {@link #buildSearchTermQuery(IIndexTypeConf, String, float)}
 * will be called.
 *
 * @param query the {@link BooleanQuery} to add the sub queries.
 * @param searchText the search text. May be blank (null or contain only white spaces).
 * @param indexType the type in which should be searched.
 * @param baseBoost highest possible boost of the query. The more the match is exact
 * 			than a bigger boost will be used.
 */
public static void addSearchText(final BooleanQuery query,
		final String searchText, final IIndexTypeConf indexType, final float baseBoost) {

	if(StringUtils.isBlank(searchText)) {
		return;
	}

	query.setMinimumNumberShouldMatch(1); // at least one "should" should match
	query.add(buildSearchTermQuery(indexType, searchText, baseBoost), Occur.SHOULD);

	final BooleanQuery partsQuery = new BooleanQuery();
	query.add(partsQuery, Occur.SHOULD);

	final String[] searchTexts = searchText.toLowerCase().split("\\s");
	for (final String search : searchTexts) {
		partsQuery.add(buildSearchTermQuery(indexType, search, baseBoost), Occur.MUST); // each part has to match
	}
}
 
Example 7
Project: InComb   File: QueryUtil.java   View source code 6 votes vote down vote up
/**
 * Returns a {@link BooleanQuery} for the given search text.
 *
 * @param indexType the type in which should be searched.
 * @param search the search text.
 * @param baseBoost highest possible boost of the query. The more the match is exact
 * 			than a bigger boost will be used.
 */
private static BooleanQuery buildSearchTermQuery(final IIndexTypeConf indexType, final String search,
		final float baseBoost) {
	final BooleanQuery subQuery = new BooleanQuery();

	final String lowerCase = StringUtils.lowerCase(search);
	final String capitalized = StringUtils.capitalize(search);

	addSearchTermQueries(indexType, search, subQuery, baseBoost);

	if(!lowerCase.equals(search)) {
		addSearchTermQueries(indexType, lowerCase, subQuery, 0.8f*baseBoost);
	}

	if(!capitalized.equals(search)) {
		addSearchTermQueries(indexType, capitalized, subQuery, 0.8f*baseBoost);
	}

	return subQuery;
}
 
Example 8
Project: InComb   File: QueryUtil.java   View source code 6 votes vote down vote up
/**
 * Adds for every field in {@link IIndexTypeConf} a {@link TermQuery}, {@link PrefixQuery} and
 * a {@link FuzzyQuery} with a suitable boost relative to the given base boost.
 *
 * @param indexType the type in which should be searched.
 * @param search the search text.
 * @param subQuery the {@link BooleanQuery} to add the sub queries.
 * @param baseBoost highest possible boost of the query. The more the match is exact
 * 			than a bigger boost will be used.
 */
private static void addSearchTermQueries(final IIndexTypeConf indexType, final String search,
		final BooleanQuery subQuery, final float baseBoost) {

	for(final IIndexFieldConf<?> field : indexType.getFields()) {
		final Term term = new Term(field.getName(), search);

		final TermQuery exactQuery = new TermQuery(term);
		exactQuery.setBoost(baseBoost);
		subQuery.add(exactQuery, Occur.SHOULD);

		final PrefixQuery pfQuery = new PrefixQuery(term);
		pfQuery.setBoost(0.7f*baseBoost);
		subQuery.add(pfQuery, Occur.SHOULD);

		final FuzzyQuery fuzzyQuery = new FuzzyQuery(term);
		fuzzyQuery.setBoost(0.5f*baseBoost);
		subQuery.add(fuzzyQuery, Occur.SHOULD);
	}
}
 
Example 9
Project: InComb   File: NewsDao.java   View source code 6 votes vote down vote up
/**
 * Returns all {@link News} of the given news group except the {@link News} with the given id.
 * @param newsGroupId the news group id
 * @param exceptId the news which should not be returned
 * @return a {@link List} with all {@link News} of the requested news group except the {@link News} with the exceptId.
 */
private List<News> getNewsOfNewsGroup(final long newsGroupId, final long exceptId) {
	final BooleanQuery query = new BooleanQuery();

	QueryUtil.addTypeConf(query, NewsIndexType.getInstance());

	final NumericRangeQuery<Long> groupQuery = NumericRangeQuery.newLongRange(
			NewsIndexType.FIELD_NEWSGROUPID, newsGroupId, newsGroupId, true, true);
	query.add(groupQuery, Occur.MUST);

	// exclude news
	query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(exceptId))), Occur.MUST_NOT);

	final SearchOptions options = new SearchOptions();
	options.setSort(new Sort(ESortField.PUBLISH_DATE.getSortField(ESortOrder.DESC)));

	final DocumentsSearchResult result = IndexSearch.getInstance().search(query, options);
	return NewsIndexType.docsToNews(result.getResults());
}
 
Example 10
Project: InComb   File: NewsDao.java   View source code 6 votes vote down vote up
/**
 * Returns the amount of {@link News} which are assigned to news groups (news group id > 0)
 * for the given {@link Query}.
 */
private int getAmountOfNewsInNewsGroups(final Query filterQuery) {
	final BooleanQuery query = new BooleanQuery();
	query.add(filterQuery, Occur.MUST);

	// get only news that are in real groups (newsGroupId > 0)
	final NumericRangeQuery<Long> newsGroupFilterQuery = NumericRangeQuery.newLongRange(
			NewsIndexType.FIELD_NEWSGROUPID, 0l, null, false, true);
	query.add(newsGroupFilterQuery, Occur.MUST);

	final SearchOptions options = new SearchOptions();
	options.setMaxResults(0); // we only want the totalHits, not the results.

	final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, options);

	return topDocs.totalHits;
}
 
Example 11
Project: Equella   File: ItemSyncer.java   View source code 6 votes vote down vote up
@Override
public Collection<IndexedItem> search(IndexSearcher searcher) throws IOException
{
	BooleanQuery overall = new BooleanQuery();
	BooleanQuery collections = new BooleanQuery();
	for( Institution inst : institutions )
	{
		collections.add(
			new TermQuery(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(inst.getUniqueId()))),
			Occur.SHOULD);
	}
	overall.add(collections, Occur.MUST);
	overall.add(NumericRangeQuery.newLongRange(FreeTextQuery.FIELD_ID_RANGEABLE, firstId, lastId, true, true),
		Occur.MUST);
	searcher.search(overall, compareDates);
	return compareDates.getModifiedDocs();
}
 
Example 12
Project: elasticsearch_my   File: PercolateQueryBuilderTests.java   View source code 6 votes vote down vote up
public void testCreateMultiDocumentSearcher() throws Exception {
    int numDocs = randomIntBetween(2, 8);
    List<ParseContext.Document> docs = new ArrayList<>(numDocs);
    for (int i = 0; i < numDocs; i++) {
        docs.add(new ParseContext.Document());
    }

    Analyzer analyzer = new WhitespaceAnalyzer();
    ParsedDocument parsedDocument = new ParsedDocument(null, null, "_id", "_type", null, docs, null, null, null);
    IndexSearcher indexSearcher = PercolateQueryBuilder.createMultiDocumentSearcher(analyzer, parsedDocument);
    assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(numDocs));

    // ensure that any query get modified so that the nested docs are never included as hits:
    Query query = new MatchAllDocsQuery();
    BooleanQuery result = (BooleanQuery) indexSearcher.createNormalizedWeight(query, true).getQuery();
    assertThat(result.clauses().size(), equalTo(2));
    assertThat(result.clauses().get(0).getQuery(), sameInstance(query));
    assertThat(result.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(result.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
}
 
Example 13
Project: oscm   File: LuceneQueryBuilder.java   View source code 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 14
Project: lams   File: QueryBuilder.java   View source code 6 votes vote down vote up
/** 
 * Creates a minimum-should-match query from the query text.
 * <p>
 * @param field field name
 * @param queryText text to be passed to the analyzer
 * @param fraction of query terms {@code [0..1]} that should match 
 * @return {@code TermQuery} or {@code BooleanQuery}, based on the analysis 
 *         of {@code queryText}
 */
public Query createMinShouldMatchQuery(String field, String queryText, float fraction) {
  if (Float.isNaN(fraction) || fraction < 0 || fraction > 1) {
    throw new IllegalArgumentException("fraction should be >= 0 and <= 1");
  }
  
  // TODO: wierd that BQ equals/rewrite/scorer doesn't handle this?
  if (fraction == 1) {
    return createBooleanQuery(field, queryText, BooleanClause.Occur.MUST);
  }
  
  Query query = createFieldQuery(analyzer, BooleanClause.Occur.SHOULD, field, queryText, false, 0);
  if (query instanceof BooleanQuery) {
    BooleanQuery bq = (BooleanQuery) query;
    bq.setMinimumNumberShouldMatch((int) (fraction * bq.clauses().size()));
  }
  return query;
}
 
Example 15
Project: elasticsearch_my   File: BoolQueryBuilder.java   View source code 6 votes vote down vote up
private static void addBooleanClauses(QueryShardContext context, BooleanQuery.Builder booleanQueryBuilder,
                                      List<QueryBuilder> clauses, Occur occurs) throws IOException {
    for (QueryBuilder query : clauses) {
        Query luceneQuery = null;
        switch (occurs) {
            case MUST:
            case SHOULD:
                luceneQuery = query.toQuery(context);
                break;
            case FILTER:
            case MUST_NOT:
                luceneQuery = query.toFilter(context);
                break;
        }
        booleanQueryBuilder.add(new BooleanClause(luceneQuery, occurs));
    }
}
 
Example 16
Project: elasticsearch_my   File: ParentIdQueryBuilder.java   View source code 6 votes vote down vote up
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    DocumentMapper childDocMapper = context.getMapperService().documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type());

    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST);
    // Need to take child type into account, otherwise a child doc of different type with the same id could match
    query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER);
    return query.build();
}
 
Example 17
Project: elasticsearch_my   File: SimpleQueryParser.java   View source code 6 votes vote down vote up
@Override
public Query newDefaultQuery(String text) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String,Float> entry : weights.entrySet()) {
        try {
            Query q = createBooleanQuery(entry.getKey(), text, super.getDefaultOperator());
            if (q != null) {
                bq.add(wrapWithBoost(q, entry.getValue()), BooleanClause.Occur.SHOULD);
            }
        } catch (RuntimeException e) {
            rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
 
Example 18
Project: elasticsearch_my   File: SimpleQueryParser.java   View source code 6 votes vote down vote up
/**
 * Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally
 * lowercasing the term first
 */
@Override
public Query newFuzzyQuery(String text, int fuzziness) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String,Float> entry : weights.entrySet()) {
        final String fieldName = entry.getKey();
        try {
            final BytesRef term = getAnalyzer().normalize(fieldName, text);
            Query query = new FuzzyQuery(new Term(fieldName, term), fuzziness);
            bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
        } catch (RuntimeException e) {
            rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
 
Example 19
Project: elasticsearch_my   File: SimpleQueryParser.java   View source code 6 votes vote down vote up
/**
 * Dispatches to Lucene's SimpleQueryParser's newPrefixQuery, optionally
 * lowercasing the term first or trying to analyze terms
 */
@Override
public Query newPrefixQuery(String text) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String,Float> entry : weights.entrySet()) {
        final String fieldName = entry.getKey();
        try {
            if (settings.analyzeWildcard()) {
                Query analyzedQuery = newPossiblyAnalyzedQuery(fieldName, text);
                if (analyzedQuery != null) {
                    bq.add(wrapWithBoost(analyzedQuery, entry.getValue()), BooleanClause.Occur.SHOULD);
                }
            } else {
                Term term = new Term(fieldName, getAnalyzer().normalize(fieldName, text));
                Query query = new PrefixQuery(term);
                bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
            }
        } catch (RuntimeException e) {
            return rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
 
Example 20
Project: RelevancyFeedback   File: RelevancyFeedback.java   View source code 6 votes vote down vote up
private BooleanQuery buildMustMatchQuery(Map<String,Map<String, Flt>> fieldValues, boolean mustMatch){
    BooleanQuery.Builder qryBuilder = new BooleanQuery.Builder();
    for(Map.Entry<String,Map<String,Flt>> entry: fieldValues.entrySet()){
        String fieldName = entry.getKey();
        for(Map.Entry<String,Flt> fieldValue: entry.getValue().entrySet()){
            String value = fieldValue.getKey();
            TermQuery tq = new TermQuery(new Term(fieldName, value));
            if(mustMatch) {
                qryBuilder.add(tq, BooleanClause.Occur.MUST);
            }
            else{
                qryBuilder.add(tq, BooleanClause.Occur.MUST_NOT);
            }
        }
    }
    return qryBuilder.build();
}
 
Example 21
Project: lams   File: BooleanQueryBuilder.java   View source code 6 votes vote down vote up
@Override
public Query getQuery(Element e) throws ParserException {
  BooleanQuery bq = new BooleanQuery(DOMUtils.getAttribute(e, "disableCoord", false));
  bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
  bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));

  NodeList nl = e.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node.getNodeName().equals("Clause")) {
      Element clauseElem = (Element) node;
      BooleanClause.Occur occurs = getOccursValue(clauseElem);

      Element clauseQuery = DOMUtils.getFirstChildOrFail(clauseElem);
      Query q = factory.getQuery(clauseQuery);
      bq.add(new BooleanClause(q, occurs));
    }
  }

  return bq;
}
 
Example 22
Project: NGB-master   File: VcfFilterForm.java   View source code 6 votes vote down vote up
private void addGeneFilter(BooleanQuery.Builder builder) {
    if (genes != null && !genes.field.isEmpty()) {
        BooleanQuery.Builder genesBuilder = new BooleanQuery.Builder();
        for (int i = 0; i < genes.field.size(); i++) {
            PrefixQuery geneIdPrefixQuery = new PrefixQuery(new Term(FeatureIndexFields.GENE_ID.getFieldName(),
                    genes.field.get(i).toLowerCase()));
            PrefixQuery geneNamePrefixQuery = new PrefixQuery(
                new Term(FeatureIndexFields.GENE_NAME.getFieldName(), genes.field.get(i).toLowerCase()));
            BooleanQuery.Builder geneIdOrNameQuery = new BooleanQuery.Builder();
            geneIdOrNameQuery.add(geneIdPrefixQuery, BooleanClause.Occur.SHOULD);
            geneIdOrNameQuery.add(geneNamePrefixQuery, BooleanClause.Occur.SHOULD);

            genesBuilder.add(geneIdOrNameQuery.build(), genes.conjunction ? BooleanClause.Occur.MUST :
                    BooleanClause.Occur.SHOULD);
        }

        builder.add(genesBuilder.build(), BooleanClause.Occur.MUST);
    }
}
 
Example 23
Project: elasticsearch_my   File: PlainHighlighterTests.java   View source code 6 votes vote down vote up
public void checkGeoQueryHighlighting(Query geoQuery) throws IOException, InvalidTokenOffsetsException {
    Map analysers = new HashMap<String, Analyzer>();
    analysers.put("text", new StandardAnalyzer());
    FieldNameAnalyzer fieldNameAnalyzer = new FieldNameAnalyzer(analysers);
    Query termQuery = new TermQuery(new Term("text", "failure"));
    Query boolQuery = new BooleanQuery.Builder().add(new BooleanClause(geoQuery, BooleanClause.Occur.SHOULD))
        .add(new BooleanClause(termQuery, BooleanClause.Occur.SHOULD)).build();
    org.apache.lucene.search.highlight.Highlighter highlighter =
        new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(boolQuery));
    String fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " +
        "a failure"), "Arbitrary text field which should not cause a failure");
    assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
    Query rewritten = boolQuery.rewrite(null);
    highlighter = new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(rewritten));
    fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " +
        "a failure"), "Arbitrary text field which should not cause a failure");
    assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
}
 
Example 24
Project: ir-generalized-translation-models   File: SimilarityParserPluginIntegrationTest.java   View source code 6 votes vote down vote up
/**
 * Checks that two terms are parsed and 2 <code>{@link AugmentedTermQuery}</code> inside
 * 1 <code>{@link org.apache.lucene.search.BooleanQuery}</code> are returned.
 * The schema.xml must define an analyzer for the default field defined in solrconfig.xml
 */
public void test_QParserTwoTerms() throws Exception {

    // arrange
    SolrQueryRequest queryRequest = req("good days");

    // act
    SolrQueryResponse resp = h.queryAndResponse("/similarity-query", queryRequest);

    // assert - the only way to check that the similarity parser was used is to check
    //          the type of the query returned by the similarity parser (for a single term): AugmentedTermQuery
    BasicResultContext basicResultContext = (BasicResultContext)resp.getResponse();
    Query usedLuceneQuery = basicResultContext.getQuery();
    assertTrue(usedLuceneQuery instanceof BooleanQuery);
    BooleanQuery booleanQuery = (BooleanQuery) usedLuceneQuery;

    assertEquals(2, booleanQuery.clauses().size());
    assertTrue(booleanQuery.clauses().get(0).getQuery() instanceof AugmentedTermQuery);
    assertTrue(booleanQuery.clauses().get(1).getQuery() instanceof AugmentedTermQuery);

    // cleanup
    queryRequest.close();
}
 
Example 25
Project: elasticsearch_my   File: BoolQueryBuilderTests.java   View source code 6 votes vote down vote up
public void testMinShouldMatchFilterWithShouldClauses() throws Exception {
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.filter(new BoolQueryBuilder().must(new MatchAllQueryBuilder()).should(new MatchAllQueryBuilder()));
    Query query = boolQueryBuilder.toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(0));
    assertThat(booleanQuery.clauses().size(), equalTo(1));
    BooleanClause booleanClause = booleanQuery.clauses().get(0);
    assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertThat(booleanClause.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery innerBooleanQuery = (BooleanQuery) booleanClause.getQuery();
    //we didn't set minimum should match initially, but there are should clauses so it should be 1
    assertThat(innerBooleanQuery.getMinimumNumberShouldMatch(), equalTo(1));
    assertThat(innerBooleanQuery.clauses().size(), equalTo(2));
    BooleanClause innerBooleanClause1 = innerBooleanQuery.clauses().get(0);
    assertThat(innerBooleanClause1.getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(innerBooleanClause1.getQuery(), instanceOf(MatchAllDocsQuery.class));
    BooleanClause innerBooleanClause2 = innerBooleanQuery.clauses().get(1);
    assertThat(innerBooleanClause2.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(innerBooleanClause2.getQuery(), instanceOf(MatchAllDocsQuery.class));
}
 
Example 26
Project: Equella   File: ItemIndex.java   View source code 6 votes vote down vote up
private Query addUniqueIdClauseToQuery(Query query, SearchResults<T> itemResults, IndexReader reader)
{
	List<T> results = itemResults.getResults();
	BooleanQuery orQuery = new BooleanQuery();

	for( T t : results )
	{
		ItemIdKey itemIdKey = t.getItemIdKey();
		String[] split = itemIdKey.toString().split("/");
		String uniqueId = split[1] + "/" + split[2];

		FreeTextBooleanQuery bquery = new FreeTextBooleanQuery(false, true);
		bquery.add(new FreeTextFieldQuery(FreeTextQuery.FIELD_UNIQUE, uniqueId));
		BooleanClause convertBoolean = convertToBooleanClause(bquery, reader);
		convertBoolean.setOccur(Occur.SHOULD);
		orQuery.add(convertBoolean);
	}

	BooleanQuery newQuery = new BooleanQuery();
	newQuery.add(query, Occur.MUST);
	newQuery.add(orQuery, Occur.MUST);

	return newQuery;
}
 
Example 27
Project: elasticsearch_my   File: MultiMatchQueryTests.java   View source code 6 votes vote down vote up
public void testBlendNoTermQuery() {
    FakeFieldType ft1 = new FakeFieldType();
    ft1.setName("foo");
    FakeFieldType ft2 = new FakeFieldType() {
        @Override
        public Query termQuery(Object value, QueryShardContext context) {
            return new MatchAllDocsQuery();
        }
    };
    ft2.setName("bar");
    Term[] terms = new Term[] { new Term("foo", "baz") };
    float[] boosts = new float[] {2};
    Query expectedClause1 = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false);
    Query expectedClause2 = new BoostQuery(new MatchAllDocsQuery(), 3);
    Query expected = new BooleanQuery.Builder().setDisableCoord(true)
            .add(expectedClause1, Occur.SHOULD)
            .add(expectedClause2, Occur.SHOULD)
            .build();
    Query actual = MultiMatchQuery.blendTerm(
            indexService.newQueryShardContext(randomInt(20), null, () -> { throw new UnsupportedOperationException(); }),
            new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3));
    assertEquals(expected, actual);
}
 
Example 28
Project: incubator-netbeans   File: DocumentUtil.java   View source code 5 votes vote down vote up
private static Query createClassWithEnclosedQuery (final String resourceName, final String sourceName) {
    final BooleanQuery query = createFQNQuery(resourceName);
    if (sourceName != null) {
        query.add (new TermQuery(new Term (DocumentUtil.FIELD_SOURCE,sourceName)), Occur.MUST);
    }
    return query;
}
 
Example 29
Project: incubator-netbeans   File: QueryUtil.java   View source code 5 votes vote down vote up
static Query createUsagesQuery(
        final @NonNull String resourceName,
        final @NonNull Set<? extends ClassIndexImpl.UsageType> mask,
        final @NonNull Occur operator) {
    Parameters.notNull("resourceName", resourceName);
    Parameters.notNull("mask", mask);
    Parameters.notNull("operator", operator);
    if (operator == Occur.SHOULD) {
        final BooleanQuery query = new BooleanQuery ();
        for (ClassIndexImpl.UsageType ut : mask) {
            final Query subQuery = new WildcardQuery(
                DocumentUtil.referencesTerm (
                    resourceName,
                    EnumSet.of(ut),
                    false));
            query.add(subQuery, operator);
        }
        return query;
    } else if (operator == Occur.MUST) {
        return new WildcardQuery(
            DocumentUtil.referencesTerm (
                resourceName,
                mask,
                false));
    } else {
        throw new IllegalArgumentException();
    }
}
 
Example 30
Project: incubator-netbeans   File: Queries.java   View source code 5 votes vote down vote up
private static void attach (final BooleanQuery query, final TermCollector collector) {
    for (BooleanClause clause : query.getClauses()) {
        final Query q = clause.getQuery();
        if (!(q instanceof TermCollector.TermCollecting)) {
            throw new IllegalArgumentException();
        }
        ((TermCollector.TermCollecting)q).attach(collector);
    }
}
 
Example 31
Project: cloud-c4c-ticket-duplicate-finder-ext   File: IndexService.java   View source code 5 votes vote down vote up
private static ScoreDoc[] getTopHitsDoc(String ticketid, BooleanQuery query, IndexSearcher searcher) throws IOException, IndexException {
	ScoreDoc[] topHitsDocs = searcher.search(query, MAX_COUNT).scoreDocs;
	if (topHitsDocs.length == 0) {
		throw new IndexException(MessageFormat.format(ERROR_TICKET_NOT_FOUND, ticketid));
	}
	return topHitsDocs;
}
 
Example 32
Project: incubator-netbeans   File: ArtifactDependencyIndexCreator.java   View source code 5 votes vote down vote up
static Query query(String groupId, String artifactId, String version) {
    final BooleanQuery q = new BooleanQuery();
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_GROUP, groupId)), BooleanClause.Occur.MUST));
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_ARTIFACT, artifactId)), BooleanClause.Occur.MUST));
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_VERSION, version)), BooleanClause.Occur.MUST));
    return q;
}
 
Example 33
Project: InComb   File: IndexingThread.java   View source code 5 votes vote down vote up
/**
 * Deletes the given {@link IIndexElement} from the index.
 * @param element the {@link IIndexElement} to remove.
 * @throws IOException if an error occurred in the index.
 */
private void delete(final IIndexElement element) throws IOException {
	final IIndexTypeConf conf = indexData.getConf();

	// build query for deletion
	final BooleanQuery query = new BooleanQuery();
	query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, element.getId())), Occur.MUST);
	query.add(new TermQuery(new Term(IIndexElement.FIELD_INDEX_TYPE, conf.getName())), Occur.MUST);

	index.deleteDocuments(query);
}
 
Example 34
Project: Elasticsearch   File: MoreLikeThisQueryParser.java   View source code 5 votes vote down vote up
private static void handleExclude(BooleanQuery boolQuery, List<Item> likeItems) {
    // artificial docs get assigned a random id and should be disregarded
    List<BytesRef> uids = new ArrayList<>();
    for (Item item : likeItems) {
        if (item.doc() != null) {
            continue;
        }
        uids.add(createUidAsBytes(item.type(), item.id()));
    }
    if (!uids.isEmpty()) {
        TermsQuery query = new TermsQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[0]));
        boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
    }
}
 
Example 35
Project: elasticsearch_my   File: PercolatorFieldMapper.java   View source code 5 votes vote down vote up
Query createCandidateQuery(IndexReader indexReader) throws IOException {
    List<BytesRef> extractedTerms = new ArrayList<>();
    LeafReader reader = indexReader.leaves().get(0).reader();
    Fields fields = reader.fields();
    for (String field : fields) {
        Terms terms = fields.terms(field);
        if (terms == null) {
            continue;
        }

        BytesRef fieldBr = new BytesRef(field);
        TermsEnum tenum = terms.iterator();
        for (BytesRef term = tenum.next(); term != null; term = tenum.next()) {
            BytesRefBuilder builder = new BytesRefBuilder();
            builder.append(fieldBr);
            builder.append(FIELD_VALUE_SEPARATOR);
            builder.append(term);
            extractedTerms.add(builder.toBytesRef());
        }
    }
    Query extractionSuccess = new TermInSetQuery(queryTermsField.name(), extractedTerms);
    // include extractionResultField:failed, because docs with this term have no extractedTermsField
    // and otherwise we would fail to return these docs. Docs that failed query term extraction
    // always need to be verified by MemoryIndex:
    Query extractionFailure = new TermQuery(new Term(extractionResultField.name(), EXTRACTION_FAILED));

    return new BooleanQuery.Builder()
            .add(extractionSuccess, Occur.SHOULD)
            .add(extractionFailure, Occur.SHOULD)
            .build();
}
 
Example 36
Project: elasticsearch_my   File: QueryAnalyzerTests.java   View source code 5 votes vote down vote up
public void testExtractQueryMetadata_booleanQuery() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);

    BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
    TermQuery termQuery2 = new TermQuery(new Term("_field1", "_term"));
    subBuilder.add(termQuery2, BooleanClause.Occur.MUST);
    TermQuery termQuery3 = new TermQuery(new Term("_field3", "_long_term"));
    subBuilder.add(termQuery3, BooleanClause.Occur.MUST);
    builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat("Should clause with phrase query isn't verified, so entire query can't be verified", result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(3));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(1).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
}
 
Example 37
Project: elasticsearch_my   File: QueryAnalyzerTests.java   View source code 5 votes vote down vote up
public void testExtractQueryMetadata_booleanQuery_onlyShould() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);

    BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
    TermQuery termQuery3 = new TermQuery(new Term("_field1", "_term"));
    subBuilder.add(termQuery3, BooleanClause.Occur.SHOULD);
    TermQuery termQuery4 = new TermQuery(new Term("_field3", "_long_term"));
    subBuilder.add(termQuery4, BooleanClause.Occur.SHOULD);
    builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(4));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
    assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
    assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
    assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
}
 
Example 38
Project: elasticsearch_my   File: QueryAnalyzerTests.java   View source code 5 votes vote down vote up
public void testExtractQueryMetadata_booleanQueryWithMustNot() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.MUST_NOT);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(0).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
}
 
Example 39
Project: elasticsearch_my   File: QueryAnalyzerTests.java   View source code 5 votes vote down vote up
public void testExtractQueryMetadata_matchNoDocsQuery() {
    Result result = analyze(new MatchNoDocsQuery("sometimes there is no reason at all"));
    assertThat(result.verified, is(true));
    assertEquals(0, result.terms.size());

    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.MUST);
    bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.MUST);
    result = analyze(bq.build());
    assertThat(result.verified, is(false));
    assertEquals(0, result.terms.size());

    bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.SHOULD);
    bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.SHOULD);
    result = analyze(bq.build());
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, new Term("field", "value"));

    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(
            Arrays.asList(new TermQuery(new Term("field", "value")), new MatchNoDocsQuery("sometimes there is no reason at all")),
            1f
    );
    result = analyze(disjunctionMaxQuery);
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, new Term("field", "value"));
}
 
Example 40
Project: elasticsearch_my   File: QueryAnalyzerTests.java   View source code 5 votes vote down vote up
public void testExtractQueryMetadata_unsupportedQuery() {
    TermRangeQuery termRangeQuery = new TermRangeQuery("_field", null, null, true, false);
    UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(termRangeQuery));
    assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));

    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    builder.add(termRangeQuery, BooleanClause.Occur.SHOULD);
    BooleanQuery bq = builder.build();

    e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq));
    assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));
}