org.apache.lucene.search.WildcardQuery Java Examples

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

  // BooleanQuery prefixQueries = new BooleanQuery();
  Builder prefixQueriesBuilder = new BooleanQuery.Builder();
  for (String curie : query.getPrefixes()) {
    String prefix = curieUtil.getExpansion(curie);
    prefixQueriesBuilder.add(new WildcardQuery(new Term(CommonProperties.IRI, prefix + "*")),
        Occur.SHOULD);
  }
  if (!query.getPrefixes().isEmpty()) {
    indexQuery.add(new BooleanClause(prefixQueriesBuilder.build(), Occur.MUST));
  }
}
 
Example #9
Source File: KNearestNeighborClassifier.java    From lucene-solr with 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 #10
Source File: LuceneQueryBuilder.java    From development with 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 #11
Source File: TestLindenCore.java    From linden with 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 #12
Source File: QueryHelper.java    From fess with 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 #13
Source File: HighlighterTest.java    From lucene-solr with 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 #14
Source File: HighlighterTest.java    From lucene-solr with 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 #15
Source File: TestSpanMultiTermQueryWrapper.java    From lucene-solr with 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 #16
Source File: LuceneInMemorySearchIntegrationTest.java    From tutorials with 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 #17
Source File: TestHighlightingMatcher.java    From lucene-solr with 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 #18
Source File: SimpleNaiveBayesClassifier.java    From lucene-solr with 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 #19
Source File: TestPayloadCheckQuery.java    From lucene-solr with 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 #20
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLeadingWildcardType() throws Exception {
  CommonQueryParserConfiguration cqpC = getParserConfig(null);
  cqpC.setAllowLeadingWildcard(true);
  assertEquals(WildcardQuery.class, getQuery("t*erm*",cqpC).getClass());
  assertEquals(WildcardQuery.class, getQuery("?term*",cqpC).getClass());
  assertEquals(WildcardQuery.class, getQuery("*term*",cqpC).getClass());
}
 
Example #21
Source File: LuceneSearch.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void deleteDoc(String noteId, Paragraph p) {
  String fullNoteOrJustParagraph = formatDeleteId(noteId, p);
  logger.debug("Deleting note {}, out of: {}", noteId, indexWriter.numDocs());
  try {
    indexWriter.deleteDocuments(new WildcardQuery(new Term(ID_FIELD, fullNoteOrJustParagraph)));
    indexWriter.commit();
  } catch (IOException e) {
    logger.error("Failed to delete {} from index by '{}'", noteId, fullNoteOrJustParagraph, e);
  }
  logger.debug("Done, index contains {} docs now" + indexWriter.numDocs());
}
 
Example #22
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Ignore("flexible queryparser shouldn't escape wildcard terms")
public void testEscapedWildcard() throws Exception {
  StandardQueryParser qp = new StandardQueryParser();
  qp.setAnalyzer(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));

  WildcardQuery q = new WildcardQuery(new Term("field", "foo\\?ba?r"));
  assertEquals(q, qp.parse("foo\\?ba?r", "field"));
}
 
Example #23
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLeadingWildcardType() throws Exception {
  StandardQueryParser qp = getParser(null);
  qp.setAllowLeadingWildcard(true);
  assertEquals(WildcardQuery.class, qp.parse("t*erm*", "field").getClass());
  assertEquals(WildcardQuery.class, qp.parse("?term*", "field").getClass());
  assertEquals(WildcardQuery.class, qp.parse("*term*", "field").getClass());
}
 
Example #24
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testConstantScoreAutoRewrite() throws Exception {
  StandardQueryParser qp = new StandardQueryParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
  Query q = qp.parse("foo*bar", "field");
  assertTrue(q instanceof WildcardQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());

  q = qp.parse("foo*", "field");
  assertTrue(q instanceof PrefixQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());

  q = qp.parse("[a TO z]", "field");
  assertTrue(q instanceof TermRangeQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
}
 
Example #25
Source File: WildcardQueryNodeBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public WildcardQuery build(QueryNode queryNode) throws QueryNodeException {
  WildcardQueryNode wildcardNode = (WildcardQueryNode) queryNode;

  WildcardQuery q = new WildcardQuery(new Term(wildcardNode.getFieldAsString(),
                                               wildcardNode.getTextAsString()));
  
  MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod)queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
  if (method != null) {
    q.setRewriteMethod(method);
  }
  
  return q;
}
 
Example #26
Source File: WildcardQueryWritable.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
  float boost = in.readFloat();
  TermWritable termWritable = new TermWritable();
  termWritable.readFields(in);
  query = new WildcardQuery(termWritable.getTerm());
  query.setBoost(boost);
}
 
Example #27
Source File: SuperParserTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static void assertEqualsQuery(Query expected, Query actual) {
  assertEquals(expected.getClass(), actual.getClass());
  if (expected instanceof BooleanQuery) {
    assertEqualsBooleanQuery((BooleanQuery) expected, (BooleanQuery) actual);
  } else if (expected instanceof SuperQuery) {
    assertEqualsSuperQuery((SuperQuery) expected, (SuperQuery) actual);
  } else if (expected instanceof TermQuery) {
    assertEqualsTermQuery((TermQuery) expected, (TermQuery) actual);
  } else if (expected instanceof PrefixQuery) {
    assertEqualsPrefixQuery((PrefixQuery) expected, (PrefixQuery) actual);
  } else if (expected instanceof WildcardQuery) {
    assertEqualsWildcardQuery((WildcardQuery) expected, (WildcardQuery) actual);
  } else if (expected instanceof FuzzyQuery) {
    assertEqualsFuzzyQuery((FuzzyQuery) expected, (FuzzyQuery) actual);
  } else if (expected instanceof RegexpQuery) {
    assertEqualsRegexpQuery((RegexpQuery) expected, (RegexpQuery) actual);
  } else if (expected instanceof TermRangeQuery) {
    assertEqualsTermRangeQuery((TermRangeQuery) expected, (TermRangeQuery) actual);
  } else if (expected instanceof MatchAllDocsQuery) {
    assertEqualsMatchAllDocsQuery((MatchAllDocsQuery) expected, (MatchAllDocsQuery) actual);
  } else if (expected instanceof MultiPhraseQuery) {
    assertEqualsMultiPhraseQuery((MultiPhraseQuery) expected, (MultiPhraseQuery) actual);
  } else if (expected instanceof PhraseQuery) {
    assertEqualsPhraseQuery((PhraseQuery) expected, (PhraseQuery) actual);
  } else if (expected instanceof NumericRangeQuery<?>) {
    assertEqualsNumericRangeQuery((NumericRangeQuery<?>) expected, (NumericRangeQuery<?>) actual);
  } else {
    fail("Type [" + expected.getClass() + "] not supported");
  }
}
 
Example #28
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertQuery(final QueryContext context, final Query query, final float boost) {
    if (query instanceof TermQuery) {
        return convertTermQuery(context, (TermQuery) query, boost);
    } else if (query instanceof TermRangeQuery) {
        return convertTermRangeQuery(context, (TermRangeQuery) query, boost);
    } else if (query instanceof PhraseQuery) {
        return convertPhraseQuery(context, (PhraseQuery) query, boost);
    } else if (query instanceof FuzzyQuery) {
        return convertFuzzyQuery(context, (FuzzyQuery) query, boost);
    } else if (query instanceof PrefixQuery) {
        return convertPrefixQuery(context, (PrefixQuery) query, boost);
    } else if (query instanceof WildcardQuery) {
        return convertWildcardQuery(context, (WildcardQuery) query, boost);
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery booleanQuery = (BooleanQuery) query;
        return convertBooleanQuery(context, booleanQuery, boost);
    } else if (query instanceof MatchAllDocsQuery) {
        return QueryBuilders.matchAllQuery();
    } else if (query instanceof BoostQuery) {
        final BoostQuery boostQuery = (BoostQuery) query;
        return convertQuery(context, boostQuery.getQuery(), boostQuery.getBoost());
    }
    throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY), "Unknown q: "
            + query.getClass() + " => " + query);
}
 
Example #29
Source File: LuceneUtil.java    From localization_nifi with 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 #30
Source File: TestSpanMultiTermQueryWrapper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPrefix() throws Exception {
  WildcardQuery wq = new WildcardQuery(new Term("field", "extrem*"));
  SpanQuery swq = new SpanMultiTermQueryWrapper<>(wq);
  // will only match "jumps over extremely very lazy broxn dog"
  SpanFirstQuery sfq = new SpanFirstQuery(swq, 3);
  assertEquals(1, searcher.count(sfq));
}