[
https://issues.apache.org/jira/browse/LUCENE-5977?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14151567#comment-14151567
]
Dawid Weiss commented on LUCENE-5977:
-------------------------------------
A full example which shows the problem. Run it with -ea -- you'll get the
assertion. Run it without assertions and it'll pass.
{code}
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.analysis.CannedTokenStream;
public class OffsetIndexingBug {
public static void main(String[] args) throws Exception {
Codec.setDefault(new SimpleTextCodec());
Version version = Version.LUCENE_CURRENT;
IndexWriterConfig conf = new IndexWriterConfig(version, new
WhitespaceAnalyzer(version));
conf.setUseCompoundFile(false);
try (Directory d = new RAMDirectory()) {
try (IndexWriter iw = new IndexWriter(d, conf)) {
iw.deleteAll();
iw.commit();
Document doc = new Document();
FieldType ftype = new FieldType();
ftype.setIndexed(true);
ftype.setStored(false);
ftype.setOmitNorms(true);
ftype.setStoreTermVectors(true);
ftype.setStoreTermVectorPositions(true);
ftype.setStoreTermVectorOffsets(true);
ftype.setTokenized(true);
ftype.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
ftype.freeze();
// note "use"'s offset is negative with respect to the first field
value.
doc.add(new Field("field-foo", new CannedTokenStream(token("use", 1,
150, 160)), ftype));
doc.add(new Field("field-foo", new CannedTokenStream(token("use", 1,
50, 60)), ftype));
iw.addDocument(doc);
}
}
}
private static Token token(String image, int positionIncrement, int soffset,
int eoffset) {
Token t = new Token();
t.setPositionIncrement(positionIncrement);
t.setOffset(soffset, eoffset);
t.append(image);
return t;
}
}
{code}
> IW should safeguard against token streams returning invalid offsets for
> multi-valued fields
> -------------------------------------------------------------------------------------------
>
> Key: LUCENE-5977
> URL: https://issues.apache.org/jira/browse/LUCENE-5977
> Project: Lucene - Core
> Issue Type: Improvement
> Affects Versions: 4.9, 4.9.1, 4.10, 4.10.1
> Reporter: Dawid Weiss
> Priority: Minor
>
> We have a custom token stream that emits information about offsets of each
> token. My (wrong) assumption was that for multi-valued fields a token
> stream's offset information is magically shifted, much like this is the case
> with positions. It's not the case -- offsets should be increasing and
> monotonic across all instances of a field, even if it has custom token
> streams. So, something like this:
> {code}
> doc.add(new Field("field-foo", new CannedTokenStream(token("bar", 1,
> 150, 160)), ftype));
> doc.add(new Field("field-foo", new CannedTokenStream(token("bar", 1,
> 50, 60)), ftype));
> {code}
> where the token function is defined as:
> {code}
> token(String image, int positionIncrement, int startOffset, int endOffset)
> {code}
> will result in either a cryptic assertion thrown from IW:
> {code}
> Exception in thread "main" java.lang.AssertionError
> at
> org.apache.lucene.index.FreqProxTermsWriterPerField.writeOffsets(FreqProxTermsWriterPerField.java:99)
> {code}
> or nothing (or a codec error) if run without assertions.
> Obviously returning non-shifted offsets from subsequent token streams makes
> little sense but I wonder if it could be made more explicit (or asserted)
> that offsets need to be increasing between multiple-values. The minimum is to
> add some documentation to OffsetAttribute. I don't know if offsets should be
> shifted automatically, as it's the case with positions -- this would change
> the semantics of existing tokenizers and filters which implement such
> shifting internally already.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]