final Scorer scorer(IndexReader reader) throws IOException { if (terms.size() == 0) // optimize zero-term case return null; if (terms.size() == 1) { // optimize one-term case Term term = (Term)terms.elementAt(0); TermDocs docs = reader.termDocs(term); if (docs == null) return null; return new TermScorer(docs, reader.norms(term.field()), weight); } TermPositions[] tps = new TermPositions[terms.size()]; for (int i = 0; i < terms.size(); i++) { TermPositions p = reader.termPositions((Term)terms.elementAt(i)); if (p == null) return null; tps[i] = p; } if (slop == 0) // optimize exact case return new ExactPhraseScorer(tps, reader.norms(field), weight); else return new SloppyPhraseScorer(tps, slop, reader.norms(field), weight); }
public Set<FieldValue> getTerms(int docId, Set<String> fieldNameSet) throws IOException { TermPositions termPosition = indexReader.termPositions(); Set<FieldValue> fieldValueSet = new HashSet<FieldValue>(); for (String fieldName : fieldNameSet) { List<FieldValueItem> fieldValueItemList = new ArrayList<FieldValueItem>(); TermEnum termEnum = indexReader.terms(new Term(fieldName, "")); if (termEnum == null) continue; Term term = termEnum.term(); if (!term.field().equals(fieldName)) continue; do { term = termEnum.term(); if (!term.field().equals(fieldName)) break; termPosition.seek(term); if (!termPosition.skipTo(docId) || termPosition.doc() != docId) continue; fieldValueItemList.add(new FieldValueItem(FieldValueOriginEnum.TERM_ENUM, term.text())); } while (termEnum.next()); termEnum.close(); if (fieldValueItemList.size() > 0) fieldValueSet.add(new FieldValue(fieldName, fieldValueItemList)); } return fieldValueSet; }
@Override public Result joinTermAllMatches(Result result, TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, TreeAxis axis, int termId) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); int position = positionsBuffer[validInstances]; if (shouldLoadPayload(position, previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); boolean updatedValidInstanceFlag = false; for (int j = previous.size - 1; j >= 0 && validInstances < NodeDataBuffer.BUFFER_SIZE && position <= previous.positions[j]; j--) { byte[] start = previous.getAsBytes(j); if (logic.joinBytesWithBytes(start, 0, payloadBuffer, bufferPtr)) { if (!updatedValidInstanceFlag) { validInstances++; updatedValidInstanceFlag = true; } byte[] end = getByteArray(payloadBuffer, bufferPtr); result.addNew(start, end, axis, termId); } } } } return result; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { term.nextPosition(); bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = previous.size - 1; j >= 0 && validInstances < NodeDataBuffer.BUFFER_SIZE; j--) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPayloadBuf(payloadBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } return validInstances; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = previous.size - 1; j >= 0 && validInstances < NodeDataBuffer.BUFFER_SIZE && positionsBuffer[validInstances] <= previous.positions[j]; j--) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } return validInstances; }
@Override public Result joinTermAllMatches(Result result, TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, TreeAxis axis, int termId) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); int position = positionsBuffer[validInstances]; if (shouldLoadPayload(position, previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); boolean updatedValidInstanceFlag = false; for (int j = 0; j < previous.size() && validInstances < NodeDataBuffer.BUFFER_SIZE && position >= previous.positions[j]; j++) { byte[] start = previous.getAsBytes(j); if (logic.joinBytesWithBytes(start, 0, payloadBuffer, bufferPtr)) { if (!updatedValidInstanceFlag) { validInstances++; updatedValidInstanceFlag = true; } byte[] end = getByteArray(payloadBuffer, bufferPtr); result.addNew(start, end, axis, termId); } } } } return result; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { term.nextPosition(); bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = 0; j < previous.size() && validInstances < NodeDataBuffer.BUFFER_SIZE; j++) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPayloadBuf(payloadBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } return validInstances; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = 0; j < previous.size() && validInstances < NodeDataBuffer.BUFFER_SIZE && positionsBuffer[validInstances] >= previous.positions[j]; j++) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } return validInstances; }
/** * Devuelve los postings de un término dado * * @param term Termino a buscar para devolver sus postings * @return lista de postings de un termino */ @Override public List<Posting> getTermPostings(String term) { ArrayList<Posting> postingList = new ArrayList<>(); try { TermDocs termDocs = ireader.termDocs(new Term("content", term)); TermPositions termPositions = ireader.termPositions(new Term("content", term)); //si se usa seek termDocs se borra //termDocs.seek(new Term(term)); while(termDocs.next()) { int docId = termDocs.doc(); int freq = termDocs.freq(); ArrayList<Long> positions = new ArrayList<>(); while (termPositions.next()) { positions.add((long)termPositions.nextPosition()); } Posting p = new Posting(docId + "", freq, positions); postingList.add(p); } return postingList; } catch (IOException ex) { Logger.getLogger(LuceneIndexing.class.getName()).log(Level.SEVERE, null, ex); } return postingList; }
/** * Constructor - should use an arg object ... * * @param weight * @param root * @param level0 * @param containerScorer * @param sfps * @param followParentInLevel0 * @param selfIds * @param reader * @param similarity * @param norms * @param dictionaryService * @param repeat */ public LeafScorer(Weight weight, TermPositions root, TermPositions level0, ContainerScorer containerScorer, StructuredFieldPosition[] sfps, boolean followParentInLevel0, HashMap<String, Counter> selfIds, IndexReader reader, Similarity similarity, byte[] norms, DictionaryService dictionaryService, boolean repeat) { super(similarity); this.root = root; this.containerScorer = containerScorer; this.sfps = sfps; //A this.allNodes = allNodes; // this.tp = tp; if (selfIds == null) { this.selfIds = new HashMap<String, Counter>(); hasSelfScorer = false; } else { this.selfIds = selfIds; hasSelfScorer = true; } this.reader = reader; this.level0 = level0; this.dictionaryService = dictionaryService; this.repeat = repeat; this.followParentInLevel0 = followParentInLevel0; matchAllLeaves = allNodes(); try { initialise(); } catch (IOException e) { throw new LeafScorerException("IO Error:", e); } }
@Override public void termPositions(final FunctionUtils.ConsumerEx<TermPositions, IOException> termPositionsConsumer) throws IOException { try (final TermPositions termPositions = indexReader.termPositions()) { termPositionsConsumer.accept(termPositions); } }
@Override final public void termPositions(FunctionUtils.ConsumerEx<TermPositions, IOException> termPositionsConsumer) throws IOException, SearchLibException { checkOnline(true); final ReaderLocal reader = acquire(); try { reader.termPositions(termPositionsConsumer); } finally { release(reader); } }
/** * @param id String * @param in TermPositions */ public FilterTermPositions(String id, TermPositions in) { super(id, in); tp = in; }
@Override public TermPositions termPositions() throws IOException { return new FilterTermPositions(id, super.termPositions()); }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); if (shouldLoadPayload(positionsBuffer[validInstances], previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = previous.size - 1; j >= 0 && validInstances < NodeDataBuffer.BUFFER_SIZE; j--) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } } return validInstances; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); if (shouldLoadPayload(positionsBuffer[validInstances], previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = previous.size - 1; j >= 0 && validInstances < NodeDataBuffer.BUFFER_SIZE && positionsBuffer[validInstances] <= previous.positions[j]; j--) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } } return validInstances; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); if (shouldLoadPayload(positionsBuffer[validInstances], previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = 0; j < previous.size() && validInstances < NodeDataBuffer.BUFFER_SIZE; j++) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } } return validInstances; }
@Override public int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException { int freq = term.freq(); int bufferPtr = 0; int validInstances = 0; int val; for (int i = 0; i < freq && validInstances < NodeDataBuffer.BUFFER_SIZE; i++) { positionsBuffer[validInstances] = term.nextPosition(); if (shouldLoadPayload(positionsBuffer[validInstances], previous)) { bufferPtr = NodeDataBuffer.PAYLOAD_LENGTH * validInstances; term.getPayload(payloadBuffer, bufferPtr); val = LookaheadOptimization.WRITE; if (!optimization.equals(LookaheadOptimization.NONE) && validInstances > 0) { val = optimization.check(payloadBuffer, validInstances - 1); if (val == LookaheadOptimization.IGNORE) continue; } for (int j = 0; j < previous.size() && validInstances < NodeDataBuffer.BUFFER_SIZE && positionsBuffer[validInstances] >= previous.positions[j]; j++) { if (logic.joinBufWithBytes(previous, j, payloadBuffer, bufferPtr)) { if (val == LookaheadOptimization.WRITE) { validInstances++; } else if (val == LookaheadOptimization.OVERWRITE) { overwritePrevPositionPayloadBuf(payloadBuffer, positionsBuffer, validInstances); } if (stopAtFirst) return 1; break; } } } } return validInstances; }
public static SolrPathScorer createPathScorer(Similarity similarity, SolrPathQuery solrPathQuery, SolrIndexReader reader, Weight weight, DictionaryService dictionarySertvice, boolean repeat) throws IOException { StructuredFieldPosition last = null; if(solrPathQuery.getPathStructuredFieldPositions().size() > 0) { last = solrPathQuery.getPathStructuredFieldPositions().get(solrPathQuery.getPathStructuredFieldPositions().size() - 1); } if (solrPathQuery.getPathStructuredFieldPositions().size() == 0) { ArrayList<StructuredFieldPosition> answer = new ArrayList<StructuredFieldPosition>(2); answer.add(new SelfAxisStructuredFieldPosition()); answer.add(new SelfAxisStructuredFieldPosition()); solrPathQuery.appendQuery(answer); } for (StructuredFieldPosition sfp : solrPathQuery.getPathStructuredFieldPositions()) { if (sfp.getTermText() != null) { TermPositions p = reader.termPositions(new Term(solrPathQuery.getPathField(), sfp.getTermText())); if (p == null) return null; CachingTermPositions ctp = new CachingTermPositions(p); sfp.setCachingTermPositions(ctp); } } SolrContainerScorer cs = null; TermPositions rootContainerPositions = null; if (solrPathQuery.getPathRootTerm() != null) { rootContainerPositions = reader.termPositions(solrPathQuery.getPathRootTerm()); } if (solrPathQuery.getPathStructuredFieldPositions().size() > 0) { cs = new SolrContainerScorer(weight, rootContainerPositions, (StructuredFieldPosition[]) solrPathQuery.getPathStructuredFieldPositions().toArray(new StructuredFieldPosition[] {}), similarity, reader.norms(solrPathQuery.getPathField())); } return new SolrPathScorer(similarity, cs); }
public TermPositions termPositions() throws IOException { // TODO Auto-generated method stub throw new UnsupportedOperationException(); }
public CachingTermPositions(TermPositions delegate) { this.delegate = delegate; }
public TermPositions termPositions() throws IOException { return new LimTermPositions(this, wrapped.termPositions()); }
public TermPositions termPositions(Term term) throws IOException { return new LimTermPositions(this, wrapped.termPositions(term)); }
/** Create a new wrapper around a TermPositions */ public LimTermPositions(LimIndexReader reader, TermPositions toWrap) { this.wrapped = toWrap; this.reader = reader; }
void termPositions(FunctionUtils.ConsumerEx<TermPositions, IOException> TermPositions) throws IOException, SearchLibException;
/** * The arguments here follow the same pattern as used by the PhraseQuery. * (It has the same unused arguments) * * @param weight - * curently unsued * @param root - * the term positions for documents with multiple entries - this * may be null, or contain no matches - it specifies those things * that appear under multiple categories etc. * @param positions - * the structured field positions - where terms should appear * @param similarity - * used in the abstract scorer implementation * @param norms - * unused */ public SolrContainerScorer(Weight weight, TermPositions root, StructuredFieldPosition[] positions, Similarity similarity, byte[] norms) { super(similarity); this.weight = weight; this.positions = positions; this.norms = norms; this.root = root; }
/** * The arguments here follow the same pattern as used by the PhraseQuery. * (It has the same unused arguments) * * @param weight - * curently unsued * @param root - * the term positions for documents with multiple entries - this * may be null, or contain no matches - it specifies those things * that appear under multiple categories etc. * @param positions - * the structured field positions - where terms should appear * @param containers TermPositions * @param similarity - * used in the abstract scorer implementation * @param norms - * unused */ public ContainerScorer(Weight weight, TermPositions root, StructuredFieldPosition[] positions, TermPositions containers, Similarity similarity, byte[] norms) { super(similarity); this.weight = weight; this.positions = positions; this.norms = norms; this.root = root; this.containers = containers; }
/** * This is a fast term joining method that checks if a term occurrence * matches any of the previous node's occurrences and returns the number of * occurrences in the current term that match the previous term * * @param term * is the TermPositions object of the current term * @param previous * is the object containing the previous term's valid occurrences * @param payloadBuffer * is a buffer used in the join operation * @param positionsBuffer * is a buffer used in the join operation * @param optimization TODO * @param stopAtFirst TODO * @return the number of matches at this term * @throws IOException */ int joinTerm(TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, LookaheadOptimization optimization, boolean stopAtFirst) throws IOException;
/** * This method finds all the matching results at the current term * * @param result * stores the results so far * @param term * is the TermPositions object of the current term * @param previous * is the object containing the previous term's valid occurrences * @param payloadBuffer * is a buffer used in the join operation * @param positionsBuffer * is a buffer used in the join operation * @param axis * is the TreeAxis object containing this joinAware instance; * this is used to avoid a cyclic dependency between * TermJoinAware and TreeAxis * @param termId * stores the term Id of the end element query term * * @return returns all matches for the join * @throws IOException */ Result joinTermAllMatches(Result result, TermPositions term, NodeDataBuffer previous, byte[] payloadBuffer, int[] positionsBuffer, TreeAxis axis, int termId) throws IOException;