下面列出了怎么用org.apache.lucene.index.DocsAndPositionsEnum的API类实例代码及写法,或者点击链接到github查看源代码。
public TermDocsEnum(FlexibleQuery.FlexibleTerm term, int docFreq, DocsAndPositionsEnum postings,
Similarity.SimScorer docScorer, int field, int termPos) throws IOException {
this.doc = -1;
this.term = term;
this.postings = postings;
this.docFreq = docFreq;
this.docScorer = docScorer;
this.field = field;
this.termPos = termPos;
this.positions = new int[initPositionSize];
this.matchedPositions = new int[initPositionSize];
}
@Override
public DocsAndPositionsEnum docsAndPositions(Bits skipDocs, DocsAndPositionsEnum reuse, int flags) throws IOException {
if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed:
return null;
}
currentFrame.decodeMetaData();
return postingsReader.docsAndPositions(fieldInfo, currentFrame.termState, skipDocs, reuse, flags);
}
@Override
public DocsAndPositionsEnum docsAndPositions(Bits skipDocs, DocsAndPositionsEnum reuse, int flags) throws IOException {
if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
// Positions were not indexed:
return null;
}
assert !eof;
currentFrame.decodeMetaData();
return postingsReader.docsAndPositions(fieldInfo, currentFrame.state, skipDocs, reuse, flags);
}
public synchronized void searchWithPosition(int queryTermsSeen) {
if (null != this.reader) {
if (null != this.reader.getContext()) {
if (null != this.reader.getContext().leaves()) {
Term term = new Term("tokens", this.searchTerm);
for (AtomicReaderContext ctx : this.reader.getContext()
.leaves()) {
int base = ctx.docBase;
// SpanTermQuery spanQ = new SpanTermQuery(term);
try {
DocsAndPositionsEnum docEnum = MultiFields
.getTermPositionsEnum(ctx.reader(),
MultiFields.getLiveDocs(ctx
.reader()), "tokens", term
.bytes());
if (null != docEnum) {
int doc = DocsEnum.NO_MORE_DOCS;
while ((doc = docEnum.nextDoc()) != DocsEnum.NO_MORE_DOCS) {
long docId = doc + base;
CandidateSimInfo simInfo = null;
if (this.simMap.containsKey(docId)) {
simInfo = this.simMap.get(docId);
simInfo.similarity = simInfo.similarity
+ Math.min(freqTerm,
docEnum.freq());
} else {
if (earlierDocs.contains(docId))
continue;
Document d = SearchManager.searcher
.get(shard).getDocument(docId);
long candidateId = Long.parseLong(d
.get("id"));
// Get rid of these early -- we're only
// looking for candidates
// whose ids are smaller than the query
if (candidateId >= this.queryId) {
// System.out.println("Query " +
// this.queryId +
// ", getting rid of " +
// candidateId);
earlierDocs.add(docId);
continue; // we reject the candidate
}
simInfo = new CandidateSimInfo();
simInfo.doc = d;
simInfo.candidateSize = Integer
.parseInt(d.get("size"));
simInfo.similarity = Math.min(freqTerm,
docEnum.freq());
// System.out.println("before putting in simmap "+
// Util.debug_thread());
this.simMap.put(docId, simInfo);
// System.out.println("after putting in simmap "+
// Util.debug_thread());
}
simInfo.queryMatchPosition = queryTermsSeen;
int candidatePos = docEnum.nextPosition();
simInfo.candidateMatchPosition = candidatePos
+ docEnum.freq();
if (!Util.isSatisfyPosFilter(
this.simMap.get(docId).similarity,
this.querySize, queryTermsSeen,
simInfo.candidateSize,
simInfo.candidateMatchPosition,
this.computedThreshold)) {
// System.out.println("before removing in simmap "+
// Util.debug_thread());
this.simMap.remove(docId);
// System.out.println("after removing in simmap "+
// Util.debug_thread());
}
}
} else {
logger.trace("docEnum is null, " + base
+ ", term: " + this.searchTerm
+ Util.debug_thread());
}
} catch (Exception e) {
e.printStackTrace();
logger.error("exception caught " + e.getMessage()
+ Util.debug_thread() + " search term:"
+ this.searchTerm);
}
}
} else {
logger.debug("leaves are null, " + this.searchTerm
+ Util.debug_thread());
}
} else {
logger.debug("getContext is null, " + this.searchTerm
+ Util.debug_thread());
}
} else {
logger.debug("this.reader is null, " + this.searchTerm
+ Util.debug_thread());
}
}
protected void requestDocumentsWithWord(String word, IntObjectOpenHashMap<IntArrayList[]> positionsInDocs,
IntIntOpenHashMap docLengths, int wordId, int numberOfWords) {
DocsAndPositionsEnum docPosEnum = null;
Term term = new Term(fieldName, word);
int localDocId,
globalDocId,
baseDocId;
IntArrayList positions[];
try {
for (int i = 0; i < reader.length; i++) {
docPosEnum = reader[i].termPositionsEnum(term);
baseDocId = contexts[i].docBase;
if (docPosEnum != null) {
while (docPosEnum.nextDoc() != DocsEnum.NO_MORE_DOCS) {
localDocId = docPosEnum.docID();
globalDocId = localDocId + baseDocId;
// if this is the first word and we found a new document
if (!positionsInDocs.containsKey(globalDocId)) {
positions = new IntArrayList[numberOfWords];
positionsInDocs.put(globalDocId, positions);
} else {
positions = positionsInDocs.get(globalDocId);
}
if (positions[wordId] == null) {
positions[wordId] = new IntArrayList();
}
// Go through the positions inside this document
for (int p = 0; p < docPosEnum.freq(); ++p) {
positions[wordId].add(docPosEnum.nextPosition());
}
if (!docLengths.containsKey(globalDocId)) {
// Get the length of the document
docLengths.put(globalDocId, reader[i].document(localDocId).getField(docLengthFieldName)
.numericValue().intValue());
}
}
}
}
} catch (IOException e) {
LOGGER.error("Error while requesting documents for word \"" + word + "\".", e);
}
}
@Override
public DocsAndPositionsEnum docsAndPositions(Bits liveDocs, DocsAndPositionsEnum reuse, int flags)
throws IOException {
checkRunningState();
return _termsEnum.docsAndPositions(liveDocs, reuse, flags);
}
@Override
public DocsAndPositionsEnum docsAndPositions(Bits liveDocs, DocsAndPositionsEnum reuse, int flags)
throws IOException {
Bits secureLiveDocs = getSecureLiveDocs(liveDocs, _maxDoc, _accessControlReader);
return in.docsAndPositions(secureLiveDocs, reuse, flags);
}