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());
}
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());
}
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());
}
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);
}
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;
}
Aggregations