Search in sources :

Example 86 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project Krill by KorAP.

the class TestMultipleDistanceIndex method testQueryWithWildCard.

@Test
public void testQueryWithWildCard() throws IOException {
    // meine* /+w1:2,s0 &Erfahrung
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc5());
    ki.commit();
    // Check simple rewriting
    WildcardQuery wcquery = new WildcardQuery(new Term("tokens", "s:Meine*"));
    SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(wcquery);
    assertEquals(wcquery.toString(), "tokens:s:Meine*");
    kr = ki.search(mtq, (short) 10);
    assertEquals(4, kr.getMatches().size());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(1, kr.getMatch(0).getEndPos());
    // Check rewriting in multidistance query
    SpanQuery sq = new SpanTermQuery(new Term("tokens", "l:Erfahrung"));
    kr = ki.search(sq, (short) 10);
    assertEquals(4, kr.getMatches().size());
    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
    constraints.add(createConstraint("w", 1, 2, true, false));
    constraints.add(createConstraint("tokens", "s", 0, 0, true, false));
    SpanQuery mdsq = new SpanMultipleDistanceQuery(mtq, sq, constraints, true, true);
    assertEquals(mdsq.toString(), "spanMultipleDistance(SpanMultiTermQueryWrapper(tokens:s:Meine*), " + "tokens:l:Erfahrung, [(w[1:2], ordered, notExcluded), (s[0:0], " + "ordered, notExcluded)])");
    kr = ki.search(mdsq, (short) 10);
    assertEquals(3, kr.getMatches().size());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
    // Check skipping with multiple documents
    ki.addDoc(createFieldDoc6());
    ki.addDoc(createFieldDoc7());
    ki.addDoc(createFieldDoc8());
    ki.commit();
    kr = ki.search(mdsq, (short) 10);
    assertEquals(6, kr.getMatches().size());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 87 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project Krill by KorAP.

the class TestSampleIndex method testWildcardStarWithCollection.

@Test
public void testWildcardStarWithCollection() throws IOException {
    // meine*
    WildcardQuery wcquery = new WildcardQuery(new Term("tokens", "s:meine*"));
    SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(wcquery);
    // meine* /+w1:2,s0 &Erfahrung
    SpanQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
    krillAvailabilityAll.setSpanQuery(mdsq);
    kr = sample.search(krillAvailabilityAll);
    assertEquals(4, kr.getMatches().size());
    assertEquals("match-GOE/AGI/04846-p107-109", kr.getMatch(0).getID());
    assertEquals("QAO-NC-LOC:ids", kr.getMatch(0).getAvailability());
    assertEquals("... gelesen und erzählt hat, ich in " + "[[meine Erfahrungen]] hätte mit aufnehmen sollen. " + "heute jedoch ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("match-GOE/AGD/00000-p132566-132569", kr.getMatch(1).getID());
    assertEquals("QAO-NC-LOC:ids-NU:1", kr.getMatch(1).getAvailability());
    assertEquals("... Mannes umständlich beibringen und solches " + "durch [[meine eigne Erfahrung]] bekräftigen: das " + "alles sollte nicht gelten ...", kr.getMatch(1).getSnippetBrackets());
    assertEquals("match-GOE/AGD/00000-p161393-161396", kr.getMatch(2).getID());
    assertEquals("QAO-NC-LOC:ids-NU:1", kr.getMatch(2).getAvailability());
    assertEquals("... lassen, bis er sich zuletzt an " + "[[meine sämtlichen Erfahrungen]] und Überzeugungen " + "anschloß, in welchem Sinne ...", kr.getMatch(2).getSnippetBrackets());
    assertEquals("match-GOE/AGD/06345-p10298-10301", kr.getMatch(3).getID());
    assertEquals("QAO-NC", kr.getMatch(3).getAvailability());
    assertEquals("... bis aufs Äußerste verfolgte, und, über " + "[[meine enge Erfahrung]] hinaus, nach ähnlichen Fällen " + "in der ...", kr.getMatch(3).getSnippetBrackets());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) Term(org.apache.lucene.index.Term) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 88 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project Krill by KorAP.

the class TestWildcardIndex method testWildcardStarWithCollection.

@Test
public void testWildcardStarWithCollection() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc1());
    ki.commit();
    // meine*
    WildcardQuery wcquery = new WildcardQuery(new Term("tokens", "s:meine*"));
    SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(wcquery);
    // meine* /+w1:2,s0 &Erfahrung
    SpanQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
    kr = ki.search(mdsq, (short) 10);
    assertEquals(4, kr.getMatches().size());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 89 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project webcert by sklintyg.

the class DiagnosRepositoryImpl method searchDiagnosisByDescription.

@Override
public List<Diagnos> searchDiagnosisByDescription(String searchString, int nbrOfResults) {
    if (Strings.isNullOrEmpty(searchString)) {
        return Collections.emptyList();
    }
    BooleanQuery query = new BooleanQuery();
    try (StandardAnalyzer analyzer = new StandardAnalyzer()) {
        TokenStream tokenStream = analyzer.tokenStream(DESC, searchString);
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            String term = WildcardQuery.WILDCARD_STRING + charTermAttribute.toString() + WildcardQuery.WILDCARD_STRING;
            query.add(new WildcardQuery(new Term(DESC, term)), BooleanClause.Occur.MUST);
        }
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred in lucene index search", e);
    }
    return searchDiagnosisByQuery(query, nbrOfResults);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Example 90 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project kie-wb-common by kiegroup.

the class AbstractQueryBuilder method getQuery.

public Query getQuery(ValueIndexTerm valueTerm) {
    final String text = getText(valueTerm);
    Term term = new Term(valueTerm.getTerm(), text);
    Query termQuery;
    switch(valueTerm.getSearchType()) {
        case PREFIX:
            termQuery = new PrefixQuery(term);
            break;
        case WILDCARD:
            termQuery = new WildcardQuery(term);
            break;
        case REGEXP:
            // NONE until there's a specific reason to use extend regex syntax
            termQuery = new RegexpQuery(term, RegExp.NONE);
            break;
        case NORMAL:
            termQuery = new TermQuery(term);
            break;
        default:
            throw new UnsupportedOperationException(ValueIndexTerm.TermSearchType.class.getSimpleName() + " value " + valueTerm.getSearchType().toString() + " is unsupported!");
    }
    return termQuery;
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) ValueIndexTerm(org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueIndexTerm) Term(org.apache.lucene.index.Term) ValueIndexTerm(org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueIndexTerm) RegexpQuery(org.apache.lucene.search.RegexpQuery)

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