List of usage examples for org.apache.lucene.search ScoreMode COMPLETE_NO_SCORES
ScoreMode COMPLETE_NO_SCORES
To view the source code for org.apache.lucene.search ScoreMode COMPLETE_NO_SCORES.
Click Source Link
From source file:org.tallison.lucene.queryparser.spans.SQPTestBase.java
License:Apache License
long countSpans(String field, Query q) throws Exception { List<LeafReaderContext> ctxs = reader.leaves(); assert (ctxs.size() == 1); LeafReaderContext leafReaderContext = ctxs.get(0); SpanQuery sq = convert(field, q);/*from w w w . j ava 2 s.c o m*/ sq = (SpanQuery) sq.rewrite(reader); SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); final Spans spans = sw.getSpans(leafReaderContext, SpanWeight.Postings.POSITIONS); long i = 0; if (spans != null) { while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { i++; } } } return i; }
From source file:org.tallison.lucene.queryparser.spans.SQPTestBase.java
License:Apache License
long countDocs(String field, Query q) throws Exception { BitSet docs = new BitSet(); List<LeafReaderContext> ctxs = reader.leaves(); assert (ctxs.size() == 1); LeafReaderContext leafReaderContext = ctxs.get(0); SpanQuery sq = convert(field, q);//from w w w .ja v a 2s . c om sq = (SpanQuery) sq.rewrite(reader); SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); final Spans spans = sw.getSpans(leafReaderContext, SpanWeight.Postings.POSITIONS); if (spans != null) { while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { docs.set(spans.docID()); } } } long spanDocHits = docs.cardinality(); // double check with a regular searcher and original query TotalHitCountCollector coll = new TotalHitCountCollector(); searcher.search(q, coll); assertEquals(coll.getTotalHits(), spanDocHits); return spanDocHits; }
From source file:org.tallison.lucene.queryparser.spans.TestSpanOnlyQueryParser.java
License:Apache License
private void testOffsetForSingleSpanMatch(SpanOnlyParser p, String s, int trueDocID, int trueSpanStart, int trueSpanEnd) throws Exception { SpanQuery sq = (SpanQuery) p.parse(s); List<LeafReaderContext> ctxs = reader.leaves(); assert (ctxs.size() == 1); LeafReaderContext ctx = ctxs.get(0); sq = (SpanQuery) sq.rewrite(ctx.reader()); SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); final Spans spans = sw.getSpans(ctx, SpanWeight.Postings.POSITIONS); int i = 0;/*from www .j av a 2 s. c o m*/ int spanStart = -1; int spanEnd = -1; int docID = -1; while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { spanStart = spans.startPosition(); spanEnd = spans.endPosition(); docID = spans.docID(); i++; } } assertEquals("should only be one matching span", 1, i); assertEquals("doc id", trueDocID, docID); assertEquals("span start", trueSpanStart, spanStart); assertEquals("span end", trueSpanEnd, spanEnd); }
From source file:org.tallison.lucene.search.concordance.charoffsets.SpansCrawler.java
License:Apache License
public static void crawl(SpanQuery query, Query filter, IndexSearcher searcher, DocTokenOffsetsVisitor visitor) throws IOException, TargetTokenNotFoundException { query = (SpanQuery) query.rewrite(searcher.getIndexReader()); SpanWeight w = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); if (filter == null) { for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) { Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS); if (spans == null) { continue; }//from w ww .j av a 2s .c o m boolean cont = visitLeafReader(ctx, spans, visitor); if (!cont) { break; } } } else { filter = searcher.rewrite(filter); Weight searcherWeight = searcher.createWeight(filter, ScoreMode.COMPLETE_NO_SCORES, 1.0f); for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) { Scorer leafReaderContextScorer = searcherWeight.scorer(ctx); if (leafReaderContextScorer == null) { continue; } //Can we tell from the scorer that there were no hits? //in <= 5.x we could stop here if the filter query had no hits. Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS); if (spans == null) { continue; } DocIdSetIterator filterItr = leafReaderContextScorer.iterator(); if (filterItr == null || filterItr.equals(DocIdSetIterator.empty())) { continue; } boolean cont = visitLeafReader(ctx, spans, filterItr, visitor); if (!cont) { break; } } } }
From source file:org.tallison.lucene.search.concordance.util.SimpleTargetCounter.java
License:Apache License
/** * Simple utility class to perform basic term frequency/document frequency * counts on the individual terms within a query. This relies on * IndexReader and does not perform any concordance search/retrieval; * it is, therefore, very fast./*w ww . j av a 2 s. c o m*/ * <p> * If you want to visit more than basic terms (e.g. SpanNear), * see {@link TargetVisitor} * * @param query query * @param searcher searcher * @return target term results * @throws java.io.IOException if there is an IOException from the searcher */ public SimpleTargetTermResults searchSingleTerms(Query query, IndexSearcher searcher) throws IOException { Query tmpQ = query.rewrite(searcher.getIndexReader()); Set<Term> terms = new HashSet<>(); Weight weight = tmpQ.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); weight.extractTerms(terms); Map<String, Integer> dfs = new HashMap<>(); Map<String, Integer> tfs = new HashMap<>(); for (Term t : terms) { String targ = t.text(); int docFreq = searcher.getIndexReader().docFreq(t); if (docFreq == 0) { continue; } Integer i = new Integer(docFreq); dfs.put(targ, i); long tf = searcher.getIndexReader().totalTermFreq(t); tfs.put(targ, (int) tf); } SimpleTargetTermResults results = new SimpleTargetTermResults(dfs, tfs); return results; }
From source file:org.tallison.lucene.search.queries.SpanQueryConverter.java
License:Apache License
@Override protected SpanQuery convertUnknownQuery(String field, Query query) { if (query instanceof CommonTermsQuery) { // specialized since rewriting would change the result query // this query is TermContext sensitive. CommonTermsQuery ctq = (CommonTermsQuery) query; Set<Term> terms = new HashSet<>(); try {/*from ww w . ja va 2 s . com*/ Weight w = ctq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f); w.extractTerms(terms); } catch (IOException e) { throw new RuntimeException("IOException on searcher!!!", e); } List<SpanQuery> spanQs = new LinkedList<SpanQuery>(); for (Term term : terms) { if (term.field().equals(field)) { spanQs.add(new SpanTermQuery(term)); } } if (spanQs.size() == 0) { return getEmptySpanQuery(); } else if (spanQs.size() == 1) { return spanQs.get(0); } else { return new SpanOrQuery(spanQs.toArray(new SpanQuery[spanQs.size()])); } } super.convertUnknownQuery(field, query); return null; }