org.apache.lucene.search.spans.SpanWeight Java Examples
The following examples show how to use
org.apache.lucene.search.spans.SpanWeight.
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: CodecCollector.java From mtas with Apache License 2.0 | 6 votes |
/** * Collect spans for occurences. * * @param occurences * the occurences * @param prefixes * the prefixes * @param field * the field * @param searcher * the searcher * @param lrc * the lrc * @return the map * @throws IOException * Signals that an I/O exception has occurred. */ private static Map<GroupHit, Spans> collectSpansForOccurences( Set<GroupHit> occurences, Set<String> prefixes, String field, IndexSearcher searcher, LeafReaderContext lrc) throws IOException { Map<GroupHit, Spans> list = new HashMap<>(); IndexReader reader = searcher.getIndexReader(); final float boost = 0; for (GroupHit hit : occurences) { MtasSpanQuery queryHit = createQueryFromGroupHit(prefixes, field, hit); if (queryHit != null) { MtasSpanQuery queryHitRewritten = queryHit.rewrite(reader); SpanWeight weight = queryHitRewritten.createWeight(searcher, false, boost); Spans spans = weight.getSpans(lrc, SpanWeight.Postings.POSITIONS); if (spans != null) { list.put(hit, spans); } } } return list; }
Example #2
Source File: PayloadScoreQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
@Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { PayloadSpanScorer scorer = (PayloadSpanScorer)scorer(context); if (scorer == null || scorer.iterator().advance(doc) != doc) return Explanation.noMatch("No match"); scorer.score(); // force freq calculation Explanation payloadExpl = scorer.getPayloadExplanation(); if (includeSpanScore) { SpanWeight innerWeight = ((PayloadSpanWeight) scorer.getWeight()).innerWeight; Explanation innerExpl = innerWeight.explain(context, doc); return Explanation.match(scorer.scoreCurrentDoc(), "PayloadSpanQuery, product of:", innerExpl, payloadExpl); } return scorer.getPayloadExplanation(); }
Example #3
Source File: TestPayloadSpans.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testSpanNot() throws Exception { SpanQuery[] clauses = new SpanQuery[2]; clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one")); clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three")); SpanQuery spq = new SpanNearQuery(clauses, 5, true); SpanNotQuery snq = new SpanNotQuery(spq, new SpanTermQuery(new Term(PayloadHelper.FIELD, "two"))); Directory directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new PayloadAnalyzer()).setSimilarity(similarity)); Document doc = new Document(); doc.add(newTextField(PayloadHelper.FIELD, "one two three one four three", Field.Store.YES)); writer.addDocument(doc); IndexReader reader = getOnlyLeafReader(writer.getReader()); writer.close(); checkSpans(snq.createWeight(newSearcher(reader, false), ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(reader.leaves().get(0), SpanWeight.Postings.PAYLOADS), 1, new int[]{2}); reader.close(); directory.close(); }
Example #4
Source File: TestPayloadTermQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
public void test() throws IOException { SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term("field", "seventy")), new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER); TopDocs hits = searcher.search(query, 100); assertTrue("hits is null and it shouldn't be", hits != null); assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100); //they should all have the exact same score, because they all contain seventy once, and we set //all the other similarity factors to be 1 for (int i = 0; i < hits.scoreDocs.length; i++) { ScoreDoc doc = hits.scoreDocs[i]; assertTrue(doc.score + " does not equal: " + 1, doc.score == 1); } CheckHits.checkExplanations(query, PayloadHelper.FIELD, searcher, true); Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS); assertTrue("spans is null and it shouldn't be", spans != null); /*float score = hits.score(0); for (int i =1; i < hits.length(); i++) { assertTrue("scores are not equal and they should be", score == hits.score(i)); }*/ }
Example #5
Source File: MtasSpanPrecededByQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanPrecededByQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanPrecededByQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #6
Source File: MtasSpanFullyAlignedWithQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanFullyAlignedWithQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanFullyAlignedWithQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #7
Source File: MtasSpanSequenceQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanSequenceQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanSequenceQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #8
Source File: MtasSpanSequenceQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { List<MtasSpanSequenceQueryWeight> subWeights = new ArrayList<>(); SpanWeight ignoreWeight = null; for (MtasSpanSequenceItem item : items) { subWeights.add(new MtasSpanSequenceQueryWeight( item.getQuery().createWeight(searcher, false, boost), item.isOptional())); } if (ignoreQuery != null) { ignoreWeight = ignoreQuery.createWeight(searcher, false, boost); } return new SpanSequenceWeight(subWeights, ignoreWeight, maximumIgnoreLength, searcher, needsScores ? getTermContexts(subWeights) : null, boost); }
Example #9
Source File: MtasExpandSpanQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight subWeight = query.createWeight(searcher, needsScores, boost); if (maximumLeft == 0 && maximumRight == 0) { return subWeight; } else { return new MtasExpandWeight(subWeight, searcher, needsScores, boost); } }
Example #10
Source File: MtasSpanFollowedByQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanFollowedByQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanFollowedByQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #11
Source File: MtasMaximumExpandSpanQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight subWeight = query.createWeight(searcher, needsScores, boost); if (maximumLeft == 0 && maximumRight == 0) { return subWeight; } else { return new MtasMaximumExpandWeight(subWeight, searcher, needsScores, boost); } }
Example #12
Source File: MtasSpanUniquePositionQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight subWeight = clause.createWeight(searcher, false, boost); return new SpanUniquePositionWeight(subWeight, searcher, needsScores ? getTermContexts(subWeight) : null, boost); }
Example #13
Source File: MtasExtendedSpanTermQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { final TermContext context; final IndexReaderContext topContext = searcher.getTopReaderContext(); if (termContext == null) { context = TermContext.build(topContext, localTerm); } else { context = termContext; } return new SpanTermWeight(context, searcher, needsScores ? Collections.singletonMap(localTerm, context) : null, boost); }
Example #14
Source File: MtasDisabledTwoPhaseIteratorSpanQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight subWeight = subQuery.createWeight(searcher, needsScores, boost); return new MtasDisabledTwoPhaseIteratorWeight(subWeight, searcher, needsScores, boost); }
Example #15
Source File: MtasSpanEndQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause)) .createWeight(searcher, needsScores, boost); return new SpanTermWeight(spanWeight, searcher, boost); }
Example #16
Source File: TestPayloadTermQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testMultipleMatchesPerDoc() throws Exception { SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")), new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER); TopDocs hits = searcher.search(query, 100); assertTrue("hits is null and it shouldn't be", hits != null); assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100); //they should all have the exact same score, because they all contain seventy once, and we set //all the other similarity factors to be 1 //System.out.println("Hash: " + seventyHash + " Twice Hash: " + 2*seventyHash); //there should be exactly 10 items that score a 4, all the rest should score a 2 //The 10 items are: 70 + i*100 where i in [0-9] int numTens = 0; for (int i = 0; i < hits.scoreDocs.length; i++) { ScoreDoc doc = hits.scoreDocs[i]; if (doc.doc % 10 == 0) { numTens++; assertTrue(doc.score + " does not equal: " + 4.0, doc.score == 4.0); } else { assertTrue(doc.score + " does not equal: " + 2, doc.score == 2); } } assertTrue(numTens + " does not equal: " + 10, numTens == 10); CheckHits.checkExplanations(query, "field", searcher, true); Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS); assertTrue("spans is null and it shouldn't be", spans != null); //should be two matches per document int count = 0; //100 hits times 2 matches per hit, we should have 200 in count while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { count++; } } assertTrue(count + " does not equal: " + 200, count == 200); }
Example #17
Source File: TestPayloadSpans.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testShrinkToAfterShortestMatch2() throws IOException { Directory directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new TestPayloadAnalyzer())); Document doc = new Document(); doc.add(new TextField("content", new StringReader("a b a d k f a h i k a k"))); writer.addDocument(doc); IndexReader reader = writer.getReader(); IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false); writer.close(); SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a")); SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 0, true); VerifyingCollector collector = new VerifyingCollector(); Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS); TopDocs topDocs = is.search(snq, 1); Set<String> payloadSet = new HashSet<>(); for (int i = 0; i < topDocs.scoreDocs.length; i++) { while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { collector.reset(); spans.collect(collector); for (final BytesRef payload: collector.payloads) { payloadSet.add(Term.toString(payload)); } } } } assertEquals(2, payloadSet.size()); assertTrue(payloadSet.contains("a:Noise:10")); assertTrue(payloadSet.contains("k:Noise:11")); reader.close(); directory.close(); }
Example #18
Source File: TestPayloadSpans.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testShrinkToAfterShortestMatch() throws IOException { Directory directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new TestPayloadAnalyzer())); Document doc = new Document(); doc.add(new TextField("content", new StringReader("a b c d e f g h i j a k"))); writer.addDocument(doc); IndexReader reader = writer.getReader(); IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false); writer.close(); SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a")); SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 1, true); VerifyingCollector collector = new VerifyingCollector(); Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS); TopDocs topDocs = is.search(snq, 1); Set<String> payloadSet = new HashSet<>(); for (int i = 0; i < topDocs.scoreDocs.length; i++) { while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { collector.reset(); spans.collect(collector); for (final BytesRef payload : collector.payloads) { payloadSet.add(Term.toString(payload)); } } } } assertEquals(2, payloadSet.size()); assertTrue(payloadSet.contains("a:Noise:10")); assertTrue(payloadSet.contains("k:Noise:11")); reader.close(); directory.close(); }
Example #19
Source File: TestPayloadSpans.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testHeavilyNestedSpanQuery() throws Exception { Spans spans; IndexSearcher searcher = getSearcher(); SpanQuery[] clauses = new SpanQuery[3]; clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one")); clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "two")); clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three")); SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 5, true); clauses = new SpanQuery[3]; clauses[0] = spanNearQuery; clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "five")); clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "six")); SpanNearQuery spanNearQuery2 = new SpanNearQuery(clauses, 6, true); SpanQuery[] clauses2 = new SpanQuery[2]; clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "eleven")); clauses2[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ten")); SpanNearQuery spanNearQuery3 = new SpanNearQuery(clauses2, 2, false); SpanQuery[] clauses3 = new SpanQuery[3]; clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nine")); clauses3[1] = spanNearQuery2; clauses3[2] = spanNearQuery3; SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false); spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 2, new int[]{8, 8}); closeIndexReader.close(); directory.close(); }
Example #20
Source File: TestPayloadSpans.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testFirstClauseWithoutPayload() throws Exception { Spans spans; IndexSearcher searcher = getSearcher(); SpanQuery[] clauses = new SpanQuery[3]; clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nopayload")); clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "qq")); clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ss")); SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 6, true); SpanQuery[] clauses2 = new SpanQuery[2]; clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "pp")); clauses2[1] = spanNearQuery; SpanNearQuery snq = new SpanNearQuery(clauses2, 6, false); SpanQuery[] clauses3 = new SpanQuery[2]; clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "np")); clauses3[1] = snq; SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false); spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS); assertTrue("spans is null and it shouldn't be", spans != null); checkSpans(spans, 1, new int[]{3}); closeIndexReader.close(); directory.close(); }
Example #21
Source File: MtasSpanIntersectingQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanIntersectingQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanIntersectingQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #22
Source File: MtasSpanStartQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause)) .createWeight(searcher, needsScores, boost); return new SpanTermWeight(spanWeight, searcher, boost); }
Example #23
Source File: PayloadScoreQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { SpanWeight innerWeight = wrappedQuery.createWeight(searcher, scoreMode, boost); if (!scoreMode.needsScores()) return innerWeight; return new PayloadSpanWeight(searcher, innerWeight, boost); }
Example #24
Source File: MtasSpanRecurrenceQuery.java From mtas with Apache License 2.0 | 5 votes |
@Override public MtasSpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { SpanWeight subWeight = query.createWeight(searcher, false, boost); SpanWeight ignoreWeight = null; if (ignoreQuery != null) { ignoreWeight = ignoreQuery.createWeight(searcher, false, boost); } return new SpanRecurrenceWeight(subWeight, ignoreWeight, maximumIgnoreLength, searcher, needsScores ? getTermContexts(subWeight) : null, boost); }
Example #25
Source File: MtasSpanNotQuery.java From mtas with Apache License 2.0 | 5 votes |
/** * Gets the term contexts. * * @param items the items * @return the term contexts */ protected Map<Term, TermContext> getTermContexts( List<MtasSpanNotQueryWeight> items) { List<SpanWeight> weights = new ArrayList<>(); for (MtasSpanNotQueryWeight item : items) { weights.add(item.spanWeight); } return getTermContexts(weights); }
Example #26
Source File: MtasSpanRegexpQuery.java From mtas with Apache License 2.0 | 4 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { return ((SpanQuery) searcher.rewrite(query)).createWeight(searcher, needsScores, boost); }
Example #27
Source File: MtasSpanOrQuery.java From mtas with Apache License 2.0 | 4 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { return baseQuery.createWeight(searcher, needsScores, boost); }
Example #28
Source File: MtasSpanAndQuery.java From mtas with Apache License 2.0 | 4 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { return baseQuery.createWeight(searcher, needsScores, boost); }
Example #29
Source File: CustomSpanPayloadCheckQuery.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 4 votes |
public SpanPayloadCheckWeight(IndexSearcher searcher, Map<Term, TermContext> termContexts, SpanWeight matchWeight, float boost) throws IOException { super(CustomSpanPayloadCheckQuery.this, searcher, termContexts, boost); this.matchWeight = matchWeight; }
Example #30
Source File: MtasSpanWithinQuery.java From mtas with Apache License 2.0 | 4 votes |
@Override public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException { return baseQuery.createWeight(searcher, needsScores, boost); }