public void testExtractQueryMetadata_spanTermQuery() { // the following span queries aren't exposed in the query dsl and are therefor not supported: // 1) SpanPositionRangeQuery // 2) PayloadScoreQuery // 3) SpanBoostQuery // The following span queries can't be supported because of how these queries work: // 1) SpanMultiTermQueryWrapper, not supported, because there is no support for MTQ typed queries yet. // 2) SpanContainingQuery, is kind of range of spans and we don't know what is between the little and big terms // 3) SpanWithinQuery, same reason as SpanContainingQuery // 4) FieldMaskingSpanQuery is a tricky query so we shouldn't optimize this SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); Result result = analyze(spanTermQuery1); assertThat(result.verified, is(true)); assertTermsEqual(result.terms, spanTermQuery1.getTerm()); }
@Override void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException { if (sourceQuery instanceof SpanTermQuery) { super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost); } else if (sourceQuery instanceof ConstantScoreQuery) { flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost); } else if (sourceQuery instanceof FunctionScoreQuery) { flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost); } else if (sourceQuery instanceof MultiPhrasePrefixQuery) { flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost); } else if (sourceQuery instanceof FiltersFunctionScoreQuery) { flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost); } else if (sourceQuery instanceof MultiPhraseQuery) { MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery); convertMultiPhraseQuery(0, new int[q.getTermArrays().size()], q, q.getTermArrays(), q.getPositions(), reader, flatQueries); } else if (sourceQuery instanceof BlendedTermQuery) { final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery; flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost); } else { super.flatten(sourceQuery, reader, flatQueries, boost); } }
@Override public SpanQuery toFragmentQuery () throws QueryException { if (this.isNull()) { return null; } SpanQuery sq = subquery.retrieveNode(this.retrieveNode) .toFragmentQuery(); if (sq == null) return null; if (sq instanceof SpanTermQuery) { if (subquery.isNegative()) { return sq; } else if ((startOffset == 0 || startOffset == -1) && (length == 1 || length == 0)) { // if (DEBUG) log.warn("Not SpanSubspanQuery. " + // "Creating only a SpanQuery for the subquery."); return sq; } return null; } return new SpanSubspanQuery(sq, startOffset, length, true); }
private SpanAttributeQuery createSpanAttributeQuery ( SpanQueryWrapper attrQueryWrapper) throws QueryException { SpanQuery sq = attrQueryWrapper.toFragmentQuery(); if (sq != null) { if (sq instanceof SpanAttributeQuery) return (SpanAttributeQuery) sq; if (sq instanceof SpanTermQuery) { return new SpanAttributeQuery((SpanTermQuery) sq, attrQueryWrapper.isNegative, true); } else { throw new IllegalArgumentException( "The subquery is not a SpanTermQuery."); } } return null; }
@Override public SpanQuery toFragmentQuery () throws QueryException { if (isNull || isEmpty) return null; SpanQuery sq = subquery.retrieveNode(this.retrieveNode) .toFragmentQuery(); if (sq == null) { isNull = true; return null; } if (sq instanceof SpanTermQuery) { return new SpanAttributeQuery((SpanTermQuery) sq, isNegative, true); } else { throw new IllegalArgumentException( "The subquery is not a SpanTermQuery."); } }
/** Skip to */ @Test public void testCase3 () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc0()); ki.addDoc(createFieldDoc1()); ki.addDoc(createFieldDoc3()); ki.commit(); SpanQuery sq, edq; edq = createQuery("s", "s:b", "s:c", 1, 1, true); sq = new SpanNextQuery(edq, new SpanTermQuery(new Term("tokens", "s:d"))); kr = ki.search(sq, (short) 10); assertEquals(1, kr.getTotalResults()); assertEquals(2, kr.getMatch(0).getLocalDocID()); assertEquals(2, kr.getMatch(0).startPos); assertEquals(5, kr.getMatch(0).endPos); }
/** Skip to */ @Test public void testCase4 () throws IOException { //System.out.println("testcase 4"); ki = new KrillIndex(); ki.addDoc(createFieldDoc0()); ki.addDoc(createFieldDoc1()); ki.addDoc(createFieldDoc2()); ki.commit(); SpanQuery sq = new SpanNextQuery(createQuery("s:d", "s:e", 1, 2, false), new SpanTermQuery(new Term("base", "s:f"))); kr = ki.search(sq, (short) 10); assertEquals(kr.getTotalResults(), 2); assertEquals(2, kr.getMatch(0).getLocalDocID()); assertEquals(2, kr.getMatch(0).getStartPos()); assertEquals(6, kr.getMatch(0).getEndPos()); assertEquals(3, kr.getMatch(1).getStartPos()); assertEquals(6, kr.getMatch(1).getEndPos()); }
/** * Multiple NextSpans in the same first span position */ @Test public void testCase8 () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); SpanQuery sq = new SpanNextQuery( new SpanTermQuery(new Term("base", "s:d")), createQuery("s:c", "s:e", 1, 2, false)); kr = ki.search(sq, (short) 10); assertEquals(kr.getTotalResults(), 3); assertEquals(0, kr.getMatch(1).getStartPos()); assertEquals(4, kr.getMatch(1).getEndPos()); }
/** * Check Skipto focus spans */ @Test public void testCase12 () throws IOException { ki.addDoc(TestRelationIndex.createFieldDoc0()); ki.addDoc(TestRelationIndex.createFieldDoc1()); ki.commit(); SpanRelationQuery sq = new SpanRelationQuery( new SpanTermQuery(new Term("base", ">:xip/syntax-dep_rel")), true, RelationDirection.RIGHT); sq.setSourceClass((byte) 1); SpanFocusQuery sfq = new SpanFocusQuery(sq, (byte) 1); sfq.setSorted(false); SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:c")); SpanNextQuery snq = new SpanNextQuery(stq, sfq); kr = ki.search(snq, (short) 20); assertEquals(0, kr.getMatch(0).getStartPos()); assertEquals(2, kr.getMatch(0).getEndPos()); assertEquals(5, kr.getMatch(1).getStartPos()); assertEquals(9, kr.getMatch(1).getEndPos()); // for (Match m : kr.getMatches()) { // System.out.println(m.getStartPos() + " " + m.getEndPos()); // } }
public TestSampleIndex () throws IOException { sample = getSampleIndex(); String jsonCollection = getJsonString(getClass() .getResource("/collection/availability-all.jsonld").getFile()); KrillCollection collection = new KrillCollection(jsonCollection); krillAvailabilityAll = new Krill(); krillAvailabilityAll.setCollection(collection); // &Erfahrung sq = new SpanTermQuery(new Term("tokens", "tt/l:Erfahrung")); // /+w1:2,s0 constraints = new ArrayList<DistanceConstraint>(); constraints.add(TestMultipleDistanceIndex.createConstraint("w", 1, 2, true, false)); constraints.add(TestMultipleDistanceIndex.createConstraint("tokens", "base/s:s", 0, 0, true, false)); }
/** Skip to SegmentSpan */ @Test public void testcase6 () throws IOException { ki.addDoc(createFieldDoc4()); ki.commit(); sq = new SpanNextQuery( new SpanSegmentQuery(new SpanTermQuery(new Term("base", "s:b")), new SpanTermQuery(new Term("base", "s:c"))), new SpanTermQuery(new Term("base", "s:d"))); kr = ki.search(sq, (short) 10); ki.close(); assertEquals("totalResults", kr.getTotalResults(), 2); // Match #0 assertEquals("doc-number", 0, kr.getMatch(0).getLocalDocID()); assertEquals("StartPos (0)", 4, kr.getMatch(0).startPos); assertEquals("EndPos (0)", 6, kr.getMatch(0).endPos); // Match #1 in the other atomic index assertEquals("doc-number", 0, kr.getMatch(1).getLocalDocID()); assertEquals("StartPos (0)", 0, kr.getMatch(1).startPos); assertEquals("EndPos (0)", 2, kr.getMatch(1).endPos); }
/** * same attribute types referring to different element types */ @Test public void testCase3 () throws IOException { ki.addDoc(createFieldDoc2()); ki.commit(); List<SpanQuery> sql = new ArrayList<>(); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=header")), true)); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=book")), true, true)); SpanQuery sq = new SpanWithAttributeQuery( new SpanElementQuery("base", "div"), sql, true); kr = ki.search(sq, (short) 10); assertEquals((long) 3, kr.getTotalResults()); assertEquals(1, kr.getMatch(0).getStartPos()); assertEquals(2, kr.getMatch(0).getEndPos()); assertEquals(5, kr.getMatch(1).getStartPos()); assertEquals(6, kr.getMatch(1).getEndPos()); assertEquals(6, kr.getMatch(2).getStartPos()); assertEquals(7, kr.getMatch(2).getEndPos()); }
/** * Arbitrary elements with a specific attribute. */ @Test public void testCase5 () throws IOException { ki.addDoc(createFieldDoc2()); ki.commit(); SpanAttributeQuery saq = new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=book")), true); SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(saq, true); kr = ki.search(swaq, (short) 10); assertEquals(6, kr.getTotalResults()); assertEquals(0, kr.getMatch(0).getStartPos()); assertEquals(3, kr.getMatch(0).getEndPos()); assertEquals(0, kr.getMatch(1).getStartPos()); assertEquals(5, kr.getMatch(1).getEndPos()); assertEquals(1, kr.getMatch(2).getStartPos()); assertEquals(2, kr.getMatch(2).getEndPos()); assertEquals(2, kr.getMatch(3).getStartPos()); assertEquals(5, kr.getMatch(3).getEndPos()); assertEquals(4, kr.getMatch(4).getStartPos()); assertEquals(5, kr.getMatch(4).getEndPos()); assertEquals(6, kr.getMatch(5).getStartPos()); assertEquals(7, kr.getMatch(5).getEndPos()); }
/** * Arbitrary elements with multiple attributes. */ @Test public void testCase6 () throws IOException { ki.addDoc(createFieldDoc2()); ki.commit(); List<SpanQuery> sql = new ArrayList<>(); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=header")), true)); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=book")), true)); SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(sql, true); kr = ki.search(swaq, (short) 10); assertEquals(2, kr.getTotalResults()); assertEquals(0, kr.getMatch(0).getStartPos()); assertEquals(3, kr.getMatch(0).getEndPos()); assertEquals(4, kr.getMatch(1).getStartPos()); assertEquals(5, kr.getMatch(1).getEndPos()); }
/** * Arbitrary elements with only not attributes. */ @Test(expected = IllegalArgumentException.class) public void testCase8 () throws IOException { ki.addDoc(createFieldDoc2()); ki.commit(); List<SpanQuery> sql = new ArrayList<>(); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=header")), true, true)); sql.add(new SpanAttributeQuery( new SpanTermQuery(new Term("base", "@:class=book")), true, true)); SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(sql, true); kr = ki.search(swaq, (short) 10); }
private void getSpanTermQueries(Query query, List<Query> spanTermQueries, List<Query> nonSpamTermQueries) throws IOException, InterruptedException { Query q; synchronized (query) { q = query.rewrite(indexReader); } if (q instanceof SpanTermQuery) { spanTermQueries.add(q); } else { if (q instanceof BooleanQuery) { BooleanClause[] bcs = ((BooleanQuery) q).getClauses(); for (BooleanClause bc : bcs) { if (Thread.currentThread().isInterrupted()) { throw new InterruptedException("Snippet extraction thread interrupted during boolean clauses processing"); } getSpanTermQueries(bc.getQuery(), spanTermQueries, nonSpamTermQueries); } } else { nonSpamTermQueries.add(q); } } }
/** Next and repetition */ @Test public void testCase6 () throws IOException { SpanQuery sq = new SpanNextQuery( new SpanTermQuery(new Term("tokens", "tt/p:NN")), new SpanRepetitionQuery( new SpanTermQuery(new Term("tokens", "mate/p:ADJA")), 2, 2, true)); ks = new Krill(sq); kr = ks.apply(ki); assertEquals(kr.getTotalResults(), 30223); // 1.1s SpanQuery sq2 = new SpanNextQuery(sq, new SpanTermQuery(new Term("tokens", "tt/p:NN"))); ks = new Krill(sq2); kr = ks.apply(ki); assertEquals(kr.getTotalResults(), 26607); // 1.1s }
@Test public void indexExample9 () throws IOException { KrillIndex ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); SpanQuery sq = new SpanNextQuery( new SpanOrQuery(new SpanTermQuery(new Term("base", "s:a")), new SpanTermQuery(new Term("base", "s:b"))), new SpanTermQuery(new Term("base", "s:c"))); Result kr = ki.search(sq, (short) 10); assertEquals(0, kr.getMatch(0).getStartPos()); assertEquals(2, kr.getMatch(0).getEndPos()); assertEquals(3, kr.getMatch(1).getStartPos()); assertEquals(5, kr.getMatch(1).getEndPos()); }
@Test public void testUnorderedTokenDistance () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc4()); ki.commit(); List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>(); constraints.add(createConstraint("w", 0, 5, true, false)); constraints.add(createConstraint("s", 0, 0, true, false)); SpanQuery mdq; mdq = createQuery("s:Begin", "s:Moderator", constraints, false); kr = ki.search(mdq, (short) 10); assertEquals(1, kr.getMatch(0).getStartPos()); assertEquals(7, kr.getMatch(0).getEndPos()); SpanQuery sq = new SpanDistanceQuery(mdq, new SpanTermQuery(new Term("base", "s:ruft")), new DistanceConstraint(0, 0, false, false), true); kr = ki.search(sq, (short) 10); assertEquals(1, kr.getMatch(0).getStartPos()); assertEquals(7, kr.getMatch(0).getEndPos()); }
/** OR */ @Test public void testCase3 () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc0()); ki.commit(); SpanQuery sq, sq2; // ec{1,2} sq = new SpanNextQuery(new SpanTermQuery(new Term("base", "s:e")), new SpanOrQuery(new SpanRepetitionQuery( new SpanTermQuery(new Term("base", "s:c")), 1, 1, true), new SpanRepetitionQuery( new SpanTermQuery(new Term("base", "s:b")), 1, 1, true))); kr = ki.search(sq, (short) 10); assertEquals((long) 3, kr.getTotalResults()); assertEquals(1, kr.getMatch(0).startPos); assertEquals(3, kr.getMatch(0).endPos); assertEquals(4, kr.getMatch(1).startPos); assertEquals(6, kr.getMatch(1).endPos); assertEquals(7, kr.getMatch(2).startPos); assertEquals(9, kr.getMatch(2).endPos); }
/** * Expansion exclusion : multiple documents * * @throws IOException */ @Test public void testCase6 () throws IOException { KrillIndex ki = new KrillIndex(); ki.addDoc(createFieldDoc0()); // same doc ki.addDoc(createFieldDoc1()); // only not clause ki.addDoc(createFieldDoc2()); // only main clause ki.commit(); SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:e")); SpanTermQuery notQuery = new SpanTermQuery(new Term("base", "s:d")); SpanExpansionQuery seq = new SpanExpansionQuery(stq, notQuery, 2, 3, 0, true); kr = ki.search(seq, (short) 20); // notClause.doc() > firstSpans.doc() assertEquals(7, kr.getMatch(0).getStartPos()); assertEquals(10, kr.getMatch(0).getEndPos()); assertEquals(7, kr.getMatch(1).getStartPos()); assertEquals(11, kr.getMatch(1).getEndPos()); // !hasMoreNotClause assertEquals(2, kr.getMatch(4).getLocalDocID()); assertEquals(1, kr.getMatch(4).getStartPos()); assertEquals(4, kr.getMatch(4).getEndPos()); }
/** * Relations only */ @Test public void testCase3 () throws IOException { ki.addDoc(createFieldDoc2()); ki.commit(); // child-of relations SpanRelationQuery srq = new SpanRelationQuery( new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT); kr = ki.search(srq, (short) 20); assertEquals((long) 13, kr.getTotalResults()); assertEquals(0, kr.getMatch(0).getStartPos()); assertEquals(1, kr.getMatch(0).getEndPos()); assertEquals(0, kr.getMatch(1).getStartPos()); assertEquals(1, kr.getMatch(1).getEndPos()); assertEquals(1, kr.getMatch(2).getStartPos()); assertEquals(2, kr.getMatch(2).getEndPos()); assertEquals(1, kr.getMatch(3).getStartPos()); assertEquals(7, kr.getMatch(3).getEndPos()); assertEquals(2, kr.getMatch(4).getStartPos()); assertEquals(3, kr.getMatch(4).getEndPos()); assertEquals(2, kr.getMatch(5).getStartPos()); assertEquals(4, kr.getMatch(5).getEndPos()); }
public void testExtractQueryMetadata_spanNearQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term")); SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("_field", true) .addClause(spanTermQuery1).addClause(spanTermQuery2).build(); Result result = analyze(spanNearQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery2.getTerm()); }
public void testExtractQueryMetadata_spanOrQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term")); SpanOrQuery spanOrQuery = new SpanOrQuery(spanTermQuery1, spanTermQuery2); Result result = analyze(spanOrQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery1.getTerm(), spanTermQuery2.getTerm()); }
public void testExtractQueryMetadata_spanFirstQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanFirstQuery spanFirstQuery = new SpanFirstQuery(spanTermQuery1, 20); Result result = analyze(spanFirstQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery1.getTerm()); }
public void testExtractQueryMetadata_spanNotQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term")); SpanNotQuery spanNotQuery = new SpanNotQuery(spanTermQuery1, spanTermQuery2); Result result = analyze(spanNotQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery1.getTerm()); }
@Override protected void doAssertLuceneQuery(SpanTermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { assertThat(query, instanceOf(SpanTermQuery.class)); SpanTermQuery spanTermQuery = (SpanTermQuery) query; assertThat(spanTermQuery.getTerm().field(), equalTo(queryBuilder.fieldName())); MappedFieldType mapper = context.getQueryShardContext().fieldMapper(queryBuilder.fieldName()); if (mapper != null) { Term term = ((TermQuery) mapper.termQuery(queryBuilder.value(), null)).getTerm(); assertThat(spanTermQuery.getTerm(), equalTo(term)); } else { assertThat(spanTermQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value()))); } }
public void testWithMetaDataField() throws IOException { QueryShardContext context = createShardContext(); for (String field : new String[]{"_type", "_all"}) { SpanTermQueryBuilder spanTermQueryBuilder = new SpanTermQueryBuilder(field, "toto"); Query query = spanTermQueryBuilder.toQuery(context); Query expected = new SpanTermQuery(new Term(field, "toto")); assertEquals(expected, query); } }
@Override public SpanQuery getSpanQuery(Element e) throws ParserException { String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); String value = DOMUtils.getNonBlankTextOrFail(e); SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, value)); stq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return stq; }
@Override public SpanQuery getSpanQuery(Element e) throws ParserException { String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); String value = DOMUtils.getNonBlankTextOrFail(e); List<SpanQuery> clausesList = new ArrayList<>(); TokenStream ts = null; try { ts = analyzer.tokenStream(fieldName, value); TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class); BytesRef bytes = termAtt.getBytesRef(); ts.reset(); while (ts.incrementToken()) { termAtt.fillBytesRef(); SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, BytesRef.deepCopyOf(bytes))); clausesList.add(stq); } ts.end(); SpanOrQuery soq = new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()])); soq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return soq; } catch (IOException ioe) { throw new ParserException("IOException parsing value:" + value); } finally { IOUtils.closeWhileHandlingException(ts); } }
private Query toTermQuery(RFTerm RFTerm) { if(RFTerm.hasPayload()) { return new PayloadScoreQuery(new SpanTermQuery(RFTerm.getTerm()), new AveragePayloadFunction(), false); } else{ return new TermQuery(RFTerm.getTerm()); } }
private Query spanFilter(SpanQuery query) { if (query instanceof SpanNearQuery) { return spanNearFilter((SpanNearQuery) query); } else if (query instanceof SpanNotQuery) { return spanNotFilter((SpanNotQuery) query); } else if (query instanceof SpanOrQuery) { return spanOrFilter((SpanOrQuery) query); } else if (query instanceof SpanTermQuery) { return new TermQuery(((SpanTermQuery) query).getTerm()); } else if (query instanceof SpanMultiTermQueryWrapper) { return ((SpanMultiTermQueryWrapper) query).getWrappedQuery(); } else { return new QueryWrapperFilter(query); } }
private Query toTermQuery(MLTTerm mltTerm) { if(mltTerm.hasPayload()) { return new PayloadScoreQuery(new SpanTermQuery(mltTerm.getTerm()), new AveragePayloadFunction(), false); } else{ return new TermQuery(mltTerm.getTerm()); } }
public void testQuery() { PayloadTermQuery boostingFuncTermQuery = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new MaxPayloadFunction()); QueryUtils.check(boostingFuncTermQuery); SpanTermQuery spanTermQuery = new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")); assertTrue(boostingFuncTermQuery.equals(spanTermQuery) == spanTermQuery.equals(boostingFuncTermQuery)); PayloadTermQuery boostingFuncTermQuery2 = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new AveragePayloadFunction()); QueryUtils.checkUnequal(boostingFuncTermQuery, boostingFuncTermQuery2); }
/** {@inheritDoc} */ @Override public SimpleSpanQuery clone () { SpanAttributeQuery sq = new SpanAttributeQuery( (SpanTermQuery) this.firstClause.clone(), this.negation, this.collectPayloads); sq.setBoost(getBoost()); return sq; }
@Test public void testCase1 () throws IOException { SpanDistanceQuery sdq = new SpanDistanceQuery( new SpanTermQuery(new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term("tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true, false), true); SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 0, 2, true); kr = ki.search(ssq, (short) 10); assertEquals((long) 8, kr.getTotalResults()); assertEquals(35, kr.getMatch(0).getStartPos()); assertEquals(37, kr.getMatch(0).getEndPos()); assertEquals(179, kr.getMatch(1).getStartPos()); assertEquals(181, kr.getMatch(1).getEndPos()); ssq = new SpanSubspanQuery(sdq, -2, 2, true); kr = ki.search(ssq, (short) 10); assertEquals(39, kr.getMatch(0).getStartPos()); assertEquals(41, kr.getMatch(0).getEndPos()); assertEquals(183, kr.getMatch(1).getStartPos()); assertEquals(185, kr.getMatch(1).getEndPos()); /* * for (Match km : kr.getMatches()){ * System.out.println(km.getStartPos() +","+km.getEndPos() * +km.getSnippetBrackets()); } */ }
public SpanQuery createQuery (String elementType, String x, String y, int min, int max, boolean isOrdered) { SpanElementQuery e = new SpanElementQuery("tokens", elementType); return new SpanDistanceQuery(new SpanTermQuery(new Term("tokens", x)), new SpanTermQuery(new Term("tokens", y)), new DistanceConstraint(e, min, max, isOrdered, false), true); }
public SpanQuery createQuery (String elementType, String x, String y, int minDistance, int maxDistance, boolean isOrdered) { SpanElementQuery e = new SpanElementQuery("base", elementType); return new SpanDistanceQuery(new SpanTermQuery(new Term("base", x)), new SpanTermQuery(new Term("base", y)), new DistanceConstraint( e, minDistance, maxDistance, isOrdered, false), true); }
/** * Multiple documents * Skip to */ @Test public void testCase4 () throws IOException { //System.out.println("testCase4"); ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.addDoc(createFieldDoc0()); ki.addDoc(createFieldDoc3()); ki.addDoc(createFieldDoc4()); ki.commit(); SpanQuery sq, edq; edq = createQuery("s", "s:b", "s:c", 1, 1, false); sq = new SpanNextQuery(edq, new SpanTermQuery(new Term("base", "s:e"))); kr = ki.search(sq, (short) 10); assertEquals((long) 4, kr.getTotalResults()); assertEquals(0, kr.getMatch(0).startPos); assertEquals(3, kr.getMatch(0).endPos); assertEquals(1, kr.getMatch(1).startPos); assertEquals(6, kr.getMatch(1).endPos); assertEquals(3, kr.getMatch(2).startPos); assertEquals(6, kr.getMatch(2).endPos); assertEquals(3, kr.getMatch(3).getLocalDocID()); assertEquals(0, kr.getMatch(3).startPos); assertEquals(4, kr.getMatch(3).endPos); }
public void testSparseSpan() throws IOException, InvalidTokenOffsetsException { final String TEXT = "the fox did not jump"; final Directory directory = newDirectory(); final IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false))); try { final Document document = new Document(); FieldType customType = new FieldType(TextField.TYPE_NOT_STORED); customType.setStoreTermVectorOffsets(true); customType.setStoreTermVectorPositions(true); customType.setStoreTermVectors(true); document.add(new Field(FIELD, new TokenStreamSparse(), customType)); indexWriter.addDocument(document); } finally { indexWriter.close(); } final IndexReader indexReader = DirectoryReader.open(directory); try { assertEquals(1, indexReader.numDocs()); final IndexSearcher indexSearcher = newSearcher(indexReader); final Query phraseQuery = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD, "did")), new SpanTermQuery(new Term(FIELD, "jump")) }, 0, true); TopDocs hits = indexSearcher.search(phraseQuery, 1); assertEquals(0, hits.totalHits); final Highlighter highlighter = new Highlighter( new SimpleHTMLFormatter(), new SimpleHTMLEncoder(), new QueryScorer(phraseQuery)); final TokenStream tokenStream = TokenSources .getTokenStream(indexReader.getTermVector( 0, FIELD), false); assertEquals( highlighter.getBestFragment(new TokenStreamSparse(), TEXT), highlighter.getBestFragment(tokenStream, TEXT)); } finally { indexReader.close(); directory.close(); } }