Search in sources :

Example 1 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project elasticsearch by elastic.

the class CandidateQueryTests method testDuel.

public void testDuel() throws Exception {
    List<Function<String, Query>> queryFunctions = new ArrayList<>();
    queryFunctions.add((id) -> new PrefixQuery(new Term("field", id)));
    queryFunctions.add((id) -> new WildcardQuery(new Term("field", id + "*")));
    queryFunctions.add((id) -> new CustomQuery(new Term("field", id)));
    queryFunctions.add((id) -> new SpanTermQuery(new Term("field", id)));
    queryFunctions.add((id) -> new TermQuery(new Term("field", id)));
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        return builder.build();
    });
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.MUST);
        if (randomBoolean()) {
            builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT);
        }
        if (randomBoolean()) {
            builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.MUST);
        }
        return builder.build();
    });
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.SHOULD);
        if (randomBoolean()) {
            builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT);
        }
        if (randomBoolean()) {
            builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.SHOULD);
        }
        return builder.build();
    });
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
        builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
        if (randomBoolean()) {
            builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT);
        }
        return builder.build();
    });
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
        builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
        if (randomBoolean()) {
            builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT);
        }
        return builder.build();
    });
    queryFunctions.add((id) -> {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.setMinimumNumberShouldMatch(randomIntBetween(0, 4));
        builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.SHOULD);
        builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.SHOULD);
        return builder.build();
    });
    queryFunctions.add((id) -> new MatchAllDocsQuery());
    queryFunctions.add((id) -> new MatchNoDocsQuery("no reason at all"));
    int numDocs = randomIntBetween(queryFunctions.size(), queryFunctions.size() * 3);
    List<ParseContext.Document> documents = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        String id = Integer.toString(i);
        Query query = queryFunctions.get(i % queryFunctions.size()).apply(id);
        addQuery(query, documents);
    }
    indexWriter.addDocuments(documents);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    // Disable query cache, because ControlQuery cannot be cached...
    shardSearcher.setQueryCache(null);
    for (int i = 0; i < numDocs; i++) {
        String id = Integer.toString(i);
        Iterable<? extends IndexableField> doc = Collections.singleton(new StringField("field", id, Field.Store.NO));
        MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer());
        duelRun(queryStore, memoryIndex, shardSearcher);
    }
    Iterable<? extends IndexableField> doc = Collections.singleton(new StringField("field", "value", Field.Store.NO));
    MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer());
    duelRun(queryStore, memoryIndex, shardSearcher);
    // Empty percolator doc:
    memoryIndex = new MemoryIndex();
    duelRun(queryStore, memoryIndex, shardSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) WildcardQuery(org.apache.lucene.search.WildcardQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Document(org.apache.lucene.document.Document) LongPoint(org.apache.lucene.document.LongPoint) CheckedFunction(org.elasticsearch.common.CheckedFunction) Function(java.util.function.Function) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) StringField(org.apache.lucene.document.StringField)

Example 2 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project elasticsearch by elastic.

the class WildcardQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(WildcardQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, instanceOf(WildcardQuery.class));
    WildcardQuery wildcardQuery = (WildcardQuery) query;
    assertThat(wildcardQuery.getField(), equalTo(queryBuilder.fieldName()));
    assertThat(wildcardQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
    assertThat(wildcardQuery.getTerm().text(), equalTo(queryBuilder.value()));
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery)

Example 3 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project zeppelin by apache.

the class LuceneSearch method deleteDoc.

private void deleteDoc(Note note, Paragraph p) {
    if (null == note) {
        LOG.error("Trying to delete note by reference to NULL");
        return;
    }
    String fullNoteOrJustParagraph = formatDeleteId(note.getId(), p);
    LOG.debug("Deleting note {}, out of: {}", note.getId(), writer.numDocs());
    try {
        writer.deleteDocuments(new WildcardQuery(new Term(ID_FIELD, fullNoteOrJustParagraph)));
        writer.commit();
    } catch (IOException e) {
        LOG.error("Failed to delete {} from index by '{}'", note, fullNoteOrJustParagraph, e);
    }
    LOG.debug("Done, index contains {} docs now" + writer.numDocs());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Example 4 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project intellij-community by JetBrains.

the class MavenIndicesTest method testCorrectlyClosingIndicesOnRemoteFacadeShutdown.

public void testCorrectlyClosingIndicesOnRemoteFacadeShutdown() throws Exception {
    MavenIndex i = myIndices.add("id", myRepositoryHelper.getTestDataPath("local1"), MavenIndex.Kind.LOCAL);
    myIndices.updateOrRepair(i, true, getMavenGeneralSettings(), EMPTY_MAVEN_PROCESS);
    MavenServerManager.getInstance().shutdown(true);
    initIndices();
    i = myIndices.getIndices().get(0);
    assertSearchResults(i, new WildcardQuery(new Term(MavenServerIndexer.SEARCH_TERM_COORDINATES, "*junit*")), "junit:junit:3.8.1", "junit:junit:3.8.2", "junit:junit:4.0");
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Term(org.apache.lucene.index.Term)

Example 5 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project intellij-community by JetBrains.

the class MavenIndicesTest method testSearching.

public void testSearching() throws Exception {
    MavenIndex i = myIndices.add("id", myRepositoryHelper.getTestDataPath("local1"), MavenIndex.Kind.LOCAL);
    myIndices.updateOrRepair(i, true, getMavenGeneralSettings(), EMPTY_MAVEN_PROCESS);
    assertSearchResults(i, new WildcardQuery(new Term(MavenServerIndexer.SEARCH_TERM_COORDINATES, "*junit*")), "junit:junit:3.8.1", "junit:junit:3.8.2", "junit:junit:4.0");
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Term(org.apache.lucene.index.Term)

Aggregations

WildcardQuery (org.apache.lucene.search.WildcardQuery)102 Term (org.apache.lucene.index.Term)94 BooleanQuery (org.apache.lucene.search.BooleanQuery)38 TermQuery (org.apache.lucene.search.TermQuery)38 Query (org.apache.lucene.search.Query)29 PrefixQuery (org.apache.lucene.search.PrefixQuery)27 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)23 Document (org.apache.lucene.document.Document)19 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 RegexpQuery (org.apache.lucene.search.RegexpQuery)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)16 PhraseQuery (org.apache.lucene.search.PhraseQuery)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 BoostQuery (org.apache.lucene.search.BoostQuery)15 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)15 SpanMultiTermQueryWrapper (org.apache.lucene.search.spans.SpanMultiTermQueryWrapper)15 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)15 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 IndexReader (org.apache.lucene.index.IndexReader)14 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14