Index: lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java =================================================================== --- lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java (revision 1395515) +++ lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java (working copy) @@ -1488,4 +1488,32 @@ } } } + + public void testEmptyFST() throws Exception { + final ByteSequenceOutputs outputs = ByteSequenceOutputs.getSingleton(); + FST fst = Builder.getEmptyFST(FST.INPUT_TYPE.BYTE1, outputs); + + //System.out.println("SAVE out.dot"); + //Writer w = new OutputStreamWriter(new FileOutputStream("/x/tmp/out.dot")); + //Util.toDot(fst, w, false, false); + //w.close(); + + for(int i=0;i<2;i++) { + assertNull(Util.get(fst, toIntsRef(new BytesRef(), new IntsRef()))); + assertNull(Util.get(fst, toIntsRef(new BytesRef("foo"), new IntsRef()))); + BytesRefFSTEnum fstEnum = new BytesRefFSTEnum(fst); + assertNull(fstEnum.next()); + + // Make sure it still works after save/load: + Directory dir = newDirectory(); + IndexOutput out = dir.createOutput("fst", IOContext.DEFAULT); + fst.save(out); + out.close(); + + IndexInput in = dir.openInput("fst", IOContext.DEFAULT); + fst = new FST(in, outputs); + in.close(); + dir.close(); + } + } } Index: lucene/core/src/java/org/apache/lucene/util/fst/BytesRefFSTEnum.java =================================================================== --- lucene/core/src/java/org/apache/lucene/util/fst/BytesRefFSTEnum.java (revision 1395515) +++ lucene/core/src/java/org/apache/lucene/util/fst/BytesRefFSTEnum.java (working copy) @@ -25,7 +25,7 @@ /** Enumerates all input (BytesRef) + output pairs in an * FST. * - * @lucene.experimental + * @lucene.experimental */ public final class BytesRefFSTEnum extends FSTEnum { Index: lucene/core/src/java/org/apache/lucene/util/fst/FSTEnum.java =================================================================== --- lucene/core/src/java/org/apache/lucene/util/fst/FSTEnum.java (revision 1395515) +++ lucene/core/src/java/org/apache/lucene/util/fst/FSTEnum.java (working copy) @@ -95,6 +95,10 @@ //System.out.println(" init"); upto = 1; fst.readFirstTargetArc(getArc(0), getArc(1), fstReader); + if (getArc(1).isLast() && !fst.targetHasArcs(getArc(1)) && !getArc(1).isFinal()) { + upto = 0; + return; + } } else { // pop //System.out.println(" check pop curArc target=" + arcs[upto].target + " label=" + arcs[upto].label + " isLast?=" + arcs[upto].isLast()); Index: lucene/core/src/java/org/apache/lucene/util/fst/Builder.java =================================================================== --- lucene/core/src/java/org/apache/lucene/util/fst/Builder.java (revision 1395515) +++ lucene/core/src/java/org/apache/lucene/util/fst/Builder.java (working copy) @@ -626,4 +626,16 @@ } } } + + /** Returns an FST accepting nothing. */ + public static FST getEmptyFST(FST.INPUT_TYPE inputType, Outputs outputs) throws IOException { + CompiledNode end = new CompiledNode(); + Builder b = new Builder(inputType, outputs); + FST emptyFST = new FST(inputType, outputs, false, 0); + end.node = emptyFST.addNode(new UnCompiledNode(b, 1)); + UnCompiledNode start = new UnCompiledNode(b, 0); + start.addArc(0, end); + emptyFST.finish(emptyFST.addNode(start)); + return emptyFST; + } }