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

The following examples show how to use org.apache.lucene.search.WildcardQuery. 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
public void testFilteredOutSpan() throws IOException {
  indexWriter.addDocument(newDoc("freezing cold stuff like stuff freedom of speech"));
  initReaderSearcherHighlighter();

  WildcardQuery wildcardQuery = new WildcardQuery(new Term("body", "free*"));
  SpanMultiTermQueryWrapper<WildcardQuery> wildcardSpanQuery = new SpanMultiTermQueryWrapper<>(wildcardQuery);
  SpanTermQuery termQuery = new SpanTermQuery(new Term("body", "speech"));
  SpanQuery spanQuery = new SpanNearQuery(new SpanQuery[]{wildcardSpanQuery, termQuery}, 3, false);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(spanQuery, BooleanClause.Occur.MUST)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);
  // spans' MatchesIterator exposes each underlying term; thus doesn't enclose intermediate "of"
  assertArrayEquals(new String[]{"freezing cold stuff like stuff <b>freedom</b> of <b>speech</b>"}, snippets);
}
 
Example 2
Source Project: onedev   Source File: TextQuery.java    License: MIT License 6 votes vote down vote up
@Override
protected void applyConstraints(BooleanQuery.Builder builder) {
	if (fileNames != null) {
		BooleanQuery.Builder subQueryBuilder = new BooleanQuery.Builder();
		for (String pattern: Splitter.on(",").omitEmptyStrings().trimResults().split(fileNames.toLowerCase()))
			subQueryBuilder.add(new WildcardQuery(new Term(BLOB_NAME.name(), pattern)), Occur.SHOULD);
		BooleanQuery subQuery = subQueryBuilder.build();
		if (subQuery.clauses().size() != 0)
			builder.add(subQuery, Occur.MUST);
	}

	if (regex) 
		builder.add(new RegexLiterals(term).asNGramQuery(BLOB_TEXT.name(), NGRAM_SIZE), Occur.MUST);
	else if (term.length() >= NGRAM_SIZE)
		builder.add(new NGramLuceneQuery(BLOB_TEXT.name(), term, NGRAM_SIZE), Occur.MUST);
	else 
		throw new TooGeneralQueryException();
}
 
Example 3
Source Project: netbeans   Source File: DocumentUtil.java    License: Apache License 2.0 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 4
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 5
Source Project: linden   Source File: TestLindenCore.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void likeTest() throws Exception {
  String bql = "select * from linden by field1 like \"aa*\" source boost by 2";
  LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
  Query query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
  Assert.assertTrue(query instanceof WildcardQuery);
  Assert.assertEquals("field1:aa*^2.0", query.toString());
  LindenResult result = lindenCore.search(request);
  Assert.assertEquals(1, result.getTotalHits());

  bql = "select * from linden by field1 not like \"aaa*\" source";
  request = bqlCompiler.compile(bql).getSearchRequest();
  query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
  Assert.assertTrue(query instanceof BooleanQuery);
  Assert.assertEquals("+*:* -field1:aaa*", query.toString());
  result = lindenCore.search(request);
  Assert.assertEquals(5, result.getTotalHits());
}
 
Example 6
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetWildCardFragments() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      WildcardQuery wildcardQuery = new WildcardQuery(new Term(FIELD_NAME, "k?nnedy"));
      wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
      doSearching(wildcardQuery);
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 4);
    }
  };

  helper.start();
}
 
Example 7
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetMidWildCardFragments() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      WildcardQuery wildcardQuery = new WildcardQuery(new Term(FIELD_NAME, "k*dy"));
      wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
      doSearching(wildcardQuery);
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 5);
    }
  };

  helper.start();
}
 
Example 8
Source Project: lucene-solr   Source File: TestPayloadCheckQuery.java    License: Apache License 2.0 6 votes vote down vote up
public void testRewrite() throws IOException {
  SpanMultiTermQueryWrapper<WildcardQuery> fiv = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "fiv*")));
  SpanMultiTermQueryWrapper<WildcardQuery> hund = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "hund*")));
  SpanMultiTermQueryWrapper<WildcardQuery> twent = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "twent*")));
  SpanMultiTermQueryWrapper<WildcardQuery> nin = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "nin*")));

  SpanNearQuery sq = new SpanNearQuery(new SpanQuery[] {fiv, hund, twent, nin}, 0, true);

  List<BytesRef> payloads = new ArrayList<>();
  payloads.add(new BytesRef("pos: 0"));
  payloads.add(new BytesRef("pos: 1"));
  payloads.add(new BytesRef("pos: 2"));
  payloads.add(new BytesRef("pos: 3"));

  SpanPayloadCheckQuery query = new SpanPayloadCheckQuery(sq, payloads);

  // if query wasn't rewritten properly, the query would have failed with "Rewrite first!"
  checkHits(query, new int[]{529});
}
 
Example 9
Source Project: lucene-solr   Source File: TestBlockJoinValidation.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  directory = newDirectory();
  final IndexWriterConfig config = new IndexWriterConfig(new MockAnalyzer(random()));
  final IndexWriter indexWriter = new IndexWriter(directory, config);
  for (int i = 0; i < AMOUNT_OF_SEGMENTS; i++) {
    List<Document> segmentDocs = createDocsForSegment(i);
    indexWriter.addDocuments(segmentDocs);
    indexWriter.commit();
  }
  indexReader = DirectoryReader.open(indexWriter);
  indexWriter.close();
  indexSearcher = new IndexSearcher(indexReader);
  parentsFilter = new QueryBitSetProducer(new WildcardQuery(new Term("parent", "*")));
}
 
Example 10
Source Project: lucene-solr   Source File: KNearestNeighborClassifier.java    License: Apache License 2.0 6 votes vote down vote up
private TopDocs knnSearch(String text) throws IOException {
  BooleanQuery.Builder mltQuery = new BooleanQuery.Builder();
  for (String fieldName : textFieldNames) {
    String boost = null;
    mlt.setBoost(true); //terms boost actually helps in MLT queries
    if (fieldName.contains("^")) {
      String[] field2boost = fieldName.split("\\^");
      fieldName = field2boost[0];
      boost = field2boost[1];
    }
    if (boost != null) {
      mlt.setBoostFactor(Float.parseFloat(boost));//if we have a field boost, we add it
    }
    mltQuery.add(new BooleanClause(mlt.like(fieldName, new StringReader(text)), BooleanClause.Occur.SHOULD));
    mlt.setBoostFactor(1);// restore neutral boost for next field
  }
  Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
  mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
  if (query != null) {
    mltQuery.add(query, BooleanClause.Occur.MUST);
  }
  return indexSearcher.search(mltQuery.build(), k);
}
 
Example 11
Source Project: lucene-solr   Source File: SimpleNaiveBayesClassifier.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * count the number of documents in the index having at least a value for the 'class' field
 *
 * @return the no. of documents having a value for the 'class' field
 * @throws IOException if accessing to term vectors or search fails
 */
protected int countDocsWithClass() throws IOException {
  Terms terms = MultiTerms.getTerms(this.indexReader, this.classFieldName);
  int docCount;
  if (terms == null || terms.getDocCount() == -1) { // in case codec doesn't support getDocCount
    TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
    BooleanQuery.Builder q = new BooleanQuery.Builder();
    q.add(new BooleanClause(new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))), BooleanClause.Occur.MUST));
    if (query != null) {
      q.add(query, BooleanClause.Occur.MUST);
    }
    indexSearcher.search(q.build(),
        classQueryCountCollector);
    docCount = classQueryCountCollector.getTotalHits();
  } else {
    docCount = terms.getDocCount();
  }
  return docCount;
}
 
Example 12
Source Project: lucene-solr   Source File: TestHighlightingMatcher.java    License: Apache License 2.0 6 votes vote down vote up
public void testWildcardProximityRewrites() throws Exception {
  final SpanNearQuery snq = SpanNearQuery.newOrderedNearQuery(FIELD)
      .addClause(new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term(FIELD, "term*"))))
      .addClause(new SpanTermQuery(new Term(FIELD, "foo")))
      .build();

  try (Monitor monitor = newMonitor()) {

    monitor.register(new MonitorQuery("1", snq));

    MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term1 foo"), HighlightsMatch.MATCHER);
    HighlightsMatch m = matches.matches("1");
    assertNotNull(m);
    assertEquals(2, m.getHitCount());
  }
}
 
Example 13
Source Project: tutorials   Source File: LuceneInMemorySearchIntegrationTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenSortFieldWhenSortedThenCorrect() {
    InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
    inMemoryLuceneIndex.indexDocument("Ganges", "River in India");
    inMemoryLuceneIndex.indexDocument("Mekong", "This river flows in south Asia");
    inMemoryLuceneIndex.indexDocument("Amazon", "Rain forest river");
    inMemoryLuceneIndex.indexDocument("Rhine", "Belongs to Europe");
    inMemoryLuceneIndex.indexDocument("Nile", "Longest River");

    Term term = new Term("body", "river");
    Query query = new WildcardQuery(term);

    SortField sortField = new SortField("title", SortField.Type.STRING_VAL, false);
    Sort sortByTitle = new Sort(sortField);

    List<Document> documents = inMemoryLuceneIndex.searchIndex(query, sortByTitle);
    Assert.assertEquals(4, documents.size());
    Assert.assertEquals("Amazon", documents.get(0).getField("title").stringValue());
}
 
Example 14
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 6 votes vote down vote up
public void testNoSuchMultiTermsInSpanFirst() throws Exception {
  //this hasn't been a problem  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);
 
  assertEquals(0, searcher.count(spanFirst));
  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
}
 
Example 15
Source Project: fess   Source File: QueryHelper.java    License: Apache License 2.0 6 votes vote down vote up
protected QueryBuilder convertWildcardQuery(final QueryContext context, final WildcardQuery wildcardQuery, final float boost) {
    final String field = getSearchField(context, wildcardQuery.getField());
    if (Constants.DEFAULT_FIELD.equals(field)) {
        context.addFieldLog(field, wildcardQuery.getTerm().text());
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.wildcardQuery(f, toLowercaseWildcard(wildcardQuery.getTerm().text()))
                .boost(b * boost));
    } else if (isSearchField(field)) {
        context.addFieldLog(field, wildcardQuery.getTerm().text());
        return QueryBuilders.wildcardQuery(field, toLowercaseWildcard(wildcardQuery.getTerm().text())).boost(boost);
    } else {
        final String query = wildcardQuery.getTerm().toString();
        final String origQuery = toLowercaseWildcard(query);
        context.addFieldLog(Constants.DEFAULT_FIELD, query);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.wildcardQuery(f, origQuery).boost(b * boost));
    }
}
 
Example 16
Source Project: development   Source File: LuceneQueryBuilder.java    License: Apache License 2.0 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 17
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 18
Source Project: SciGraph   Source File: VocabularyNeo4jImpl.java    License: 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 19
Source Project: FXDesktopSearch   Source File: QueryParser.java    License: Apache License 2.0 6 votes vote down vote up
private void addToBooleanQuery(
        final List<String> aTermList, final String aFieldName, final BooleanQuery.Builder aQuery, final BooleanClause.Occur aOccour)
        throws IOException {
    for (final var theTerm : aTermList) {
        if (QueryUtils.isWildCard(theTerm)) {
            aQuery.add(new WildcardQuery(new Term(aFieldName, theTerm)), aOccour);
        } else if (QueryUtils.isFuzzy(theTerm)) {
            aQuery.add(new FuzzyQuery(new Term(aFieldName, theTerm)), aOccour);
        } else {
            final var theTokenizedTerm = toToken(theTerm, aFieldName);
            if (!StringUtils.isEmpty(theTokenizedTerm)) {
                aQuery.add(new TermQuery(new Term(aFieldName, theTokenizedTerm)), aOccour);
            }
        }
    }

}
 
Example 20
/**
 * Query generation expression extracted from
 * {@link org.airsonic.player.service.SearchService#searchByName( String, String, int, int, List, Class)}.
 *
 * @param fieldName {@link FieldNames}
 * @return Query
 * @throws IOException When parsing of QueryParser fails
 */
public Query searchByName(String fieldName, String name) throws IOException {

    BooleanQuery.Builder mainQuery = new BooleanQuery.Builder();

    Analyzer analyzer = analyzerFactory.getQueryAnalyzer();

    try (TokenStream stream = analyzer.tokenStream(fieldName, name)) {
        stream.reset();
        stream.incrementToken();

        /*
         *  XXX 3.x -> 8.x :
         * In order to support wildcards,
         * QueryParser has been replaced by the following process.
         */

        /* Wildcards apply only to tail tokens **/
        while (true) {
            String token = stream.getAttribute(CharTermAttribute.class).toString();
            if (stream.incrementToken()) {
                mainQuery.add(new TermQuery(new Term(fieldName, token)), Occur.SHOULD);
            } else {
                WildcardQuery wildcardQuery = new WildcardQuery(new Term(fieldName, token.concat(ASTERISK)));
                mainQuery.add(wildcardQuery, Occur.SHOULD);
                break;
            }
        }

    }

    return mainQuery.build();
}
 
Example 21
Source Project: localization_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 luceneQuery = new BooleanQuery();
    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("?")) {
            luceneQuery.add(new BooleanClause(new WildcardQuery(new Term(searchTerm.getSearchableField().getSearchableFieldName(), searchTerm.getValue().toLowerCase())), Occur.MUST));
        } else {
            luceneQuery.add(new BooleanClause(new TermQuery(new Term(searchTerm.getSearchableField().getSearchableFieldName(), searchTerm.getValue().toLowerCase())), Occur.MUST));
        }
    }

    final Long minBytes = query.getMinFileSize() == null ? null : DataUnit.parseDataSize(query.getMinFileSize(), DataUnit.B).longValue();
    final Long maxBytes = query.getMaxFileSize() == null ? null : DataUnit.parseDataSize(query.getMaxFileSize(), DataUnit.B).longValue();
    if (minBytes != null || maxBytes != null) {
        luceneQuery.add(NumericRangeQuery.newLongRange(SearchableFields.FileSize.getSearchableFieldName(), minBytes, maxBytes, true, true), Occur.MUST);
    }

    final Long minDateTime = query.getStartDate() == null ? null : query.getStartDate().getTime();
    final Long maxDateTime = query.getEndDate() == null ? null : query.getEndDate().getTime();
    if (maxDateTime != null || minDateTime != null) {
        luceneQuery.add(NumericRangeQuery.newLongRange(SearchableFields.EventTime.getSearchableFieldName(), minDateTime, maxDateTime, true, true), Occur.MUST);
    }

    return luceneQuery;
}
 
Example 22
Source Project: onedev   Source File: SymbolQuery.java    License: MIT License 5 votes vote down vote up
@Override
protected void applyConstraints(BooleanQuery.Builder builder) {
	if (fileNames != null) {
		BooleanQuery.Builder subQueryBuilder = new BooleanQuery.Builder();
		for (String pattern: Splitter.on(",").omitEmptyStrings().trimResults().split(fileNames.toLowerCase()))
			subQueryBuilder.add(new WildcardQuery(new Term(BLOB_NAME.name(), pattern)), Occur.SHOULD);
		BooleanQuery subQuery = subQueryBuilder.build();
		if (subQuery.clauses().size() != 0)
			builder.add(subQuery, Occur.MUST);
	}

	boolean tooGeneral = true;
	for (char ch: term.toCharArray()) {
		if (ch != '?' && ch != '*') {
			tooGeneral = false;
			break;
		}
	}
	if (tooGeneral)
		throw new TooGeneralQueryException();

	if (primary != null) {
		String fieldName;
		if (primary)
			fieldName = BLOB_PRIMARY_SYMBOLS.name();
		else
			fieldName = BLOB_SECONDARY_SYMBOLS.name();
		
		builder.add(new WildcardQuery(new Term(fieldName, term.toLowerCase())), Occur.MUST);
	}
}
 
Example 23
Source Project: onedev   Source File: BlobQuery.java    License: MIT License 5 votes vote down vote up
/**
 * Get lucene query representation of this query
 * 
 * @return 
 * 			lucene query
 * @throws 
 * 			TooGeneralQueryException if supplied query term is too general to possibly cause query slow
 */
public Query asLuceneQuery() throws TooGeneralQueryException {
	BooleanQuery.Builder luceneQueryBuilder = new BooleanQuery.Builder();
	
	String directory = this.directory;
	if (directory != null) {
		if (!directory.endsWith("/"))
			directory += "/";
		luceneQueryBuilder.add(new WildcardQuery(BLOB_PATH.term(directory + "*")), Occur.MUST);
	}
	
	applyConstraints(luceneQueryBuilder);
	
	return luceneQueryBuilder.build();
}
 
Example 24
Source Project: onedev   Source File: FileQuery.java    License: MIT License 5 votes vote down vote up
@Override
protected void applyConstraints(BooleanQuery.Builder builder) {
	boolean tooGeneral = true;
	for (char ch: fileNames.toCharArray()) {
		if (ch != '?' && ch != '*' && ch != ',' && ch != '.') {
			tooGeneral = false;
			break;
		}
	}
	if (tooGeneral)
		throw new TooGeneralQueryException();
	
	builder.add(new WildcardQuery(new Term(BLOB_NAME.name(), fileNames.toLowerCase())), Occur.MUST);
}
 
Example 25
Source Project: netbeans   Source File: QueryUtil.java    License: Apache License 2.0 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 26
Source Project: airsonic   Source File: QueryFactory.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Query generation expression extracted from
 * {@link org.airsonic.player.service.SearchService#searchByName( String, String, int, int, List, Class)}.
 *
 * @param fieldName {@link FieldNames}
 * @return Query
 * @throws IOException When parsing of QueryParser fails
 */
public Query searchByName(String fieldName, String name) throws IOException {

    BooleanQuery.Builder mainQuery = new BooleanQuery.Builder();

    Analyzer analyzer = analyzerFactory.getQueryAnalyzer();

    try (TokenStream stream = analyzer.tokenStream(fieldName, name)) {
        stream.reset();
        stream.incrementToken();

        /*
         *  XXX 3.x -> 8.x :
         * In order to support wildcards,
         * QueryParser has been replaced by the following process.
         */

        /* Wildcards apply only to tail tokens **/
        while (true) {
            String token = stream.getAttribute(CharTermAttribute.class).toString();
            if (stream.incrementToken()) {
                mainQuery.add(new TermQuery(new Term(fieldName, token)), Occur.SHOULD);
            } else {
                WildcardQuery wildcardQuery = new WildcardQuery(new Term(fieldName, token.concat(ASTERISK)));
                mainQuery.add(wildcardQuery, Occur.SHOULD);
                break;
            }
        }

    }

    return mainQuery.build();
}
 
Example 27
Source Project: dremio-oss   Source File: LuceneQueryConverter.java    License: Apache License 2.0 5 votes vote down vote up
private StringBuilder escapeTextForWildcard(String text) {
  final StringBuilder sb = new StringBuilder(text.length());

  for (int i = 0; i < text.length(); i++) {
    char currentChar = text.charAt(i);
    if (specialCharactersMatcher.matches(currentChar)) {
      sb.append(WildcardQuery.WILDCARD_ESCAPE);
    }
    sb.append(currentChar);
  }

  return sb;
}
 
Example 28
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 29
Source Project: linden   Source File: WildcardQueryConstructor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
  if (lindenQuery.isSetWildcardQuery()) {
    LindenWildcardQuery lindenWildcardQuery = lindenQuery.getWildcardQuery();
    return new WildcardQuery(new Term(lindenWildcardQuery.getField(), lindenWildcardQuery.getQuery()));
  }
  return null;
}
 
Example 30
Source Project: lucene-solr   Source File: FieldTermStackTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testWildcard() throws Exception {
  makeIndexLongMV();
  FieldQuery fq = new FieldQuery( new WildcardQuery (new Term(F, "th*e")), reader, true, true );
  FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
  assertEquals (4, stack.termList.size());
  assertEquals ("the(15,18,2)", stack.pop().toString());
  assertEquals ("these(133,138,20)", stack.pop().toString());
  assertEquals ("the(153,156,23)", stack.pop().toString());
  assertEquals ("the(195,198,31)", stack.pop().toString());
}