I have attached my bug report test code.  I took what I reported last 
year to SourceForge, made some tweaks, made it a JUnit test-case, and 
added documentation, and finally verified the error is still present 
(it is).
/*
 * Created by IntelliJ IDEA.
 * User: dsmiley
 * Date: Mar 21, 2002
 * Time: 9:31:32 PM
 */

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;

import java.io.*;

import junit.framework.TestCase;


/**
 * Makes sure that the order of the Document.fields() enumeration is consistent between
 * searches.  This is implemented by doing Document.toString() which conveniently
 * iterates the enumeration which makes this test case simpler so I just need to do a
 * String comparison.  Truly, I should also be making sure that the order of the fields
 * is in a particular order, which according to the API, is the same that they were 
added.
 * <p>References:<br>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=492427";>JGuru FAQ item</a><br>
 * <a 
href="http://sf.net/tracker/index.php?func=detail&aid=451317&group_id=3922&atid=103922";
 *   >My bug report at SourceForge</a>
 *
 * @author "David Smiley" [EMAIL PROTECTED]
 */
public class TestBadFieldOrder extends TestCase
{
        private static final File idxDir = new File("test");

        public TestBadFieldOrder(String name)
        {
                super(name);
        }

        private static void log(String msg)
        {
                //System.out.println(msg);
        }

   /**
         * Summary: Create a doc (two terms), create doc2 (different terms than
         * doc), add doc1 to index, lookup doc by term "id", add doc2, lookup
         * again by term "id".
         */
        public static void testBadFieldOrder() throws IOException
        {

//--create doc & term
                log("Creating doc & term.");
                String result0 = null;
                final Document doc = new Document();
                doc.add(Field.Keyword("id", "ONE"));
                doc.add(Field.Keyword("2nd", "TWO"));
                result0 = "" + doc;//indirectly iterates fields()
                log("doc-->" + result0);

                final Term term2find = new Term("id", "ONE");

                final Document doc2 = new Document();
                doc2.add(Field.Keyword("id", "DUMMY"));

//--add doc
                log("Adding doc, close writer.");
                IndexWriter iw = new IndexWriter(idxDir, null, true);
                try {
                        iw.addDocument(doc);
                } finally {
                        iw.close();//flushes mods
                }
//--lookup doc by term
                log("Looking up doc by term:" + term2find);
                String result1 = null;
                IndexSearcher is = new IndexSearcher(idxDir.getPath());
                try {
                        Hits hits = is.search(new TermQuery(term2find));
                        if (hits.length() != 1) {
                                log("!!Found " + hits.length() + " doc, expected 1");
                                assertTrue(false);
                        } else {
                                result1 = "" + hits.doc(0);
                                log("doc-->" + result1);
                        }
                } finally {
                        is.close();
                }
//--add doc2
                log("Adding doc2, close writer.");
                iw = new IndexWriter(idxDir, null, false);
                try {
                        iw.addDocument(doc2);
                } finally {
                        iw.close();//flushes mods
                }
//--lookup doc by term
                log("Looking up doc by term:" + term2find);
                String result2 = null;
                is = new IndexSearcher(idxDir.getPath());
                try {
                        Hits hits = is.search(new TermQuery(term2find));
                        if (hits.length() != 1) {
                                log("!!Found " + hits.length() + " doc, expected 1");
                                assertTrue(false);
                        } else {
                                result2 = "" + hits.doc(0);
                                log("doc-->" + result2);
                        }
                } finally {
                        is.close();
                }
//--test assertion
                //example result: "Document<Keyword<2nd:TWO> Keyword<id:ONE>>"
                assertEquals(result0, result1);
                assertEquals(result1, result2);
        }

}


~ David Smiley

On Tuesday, March 19, 2002, at 10:52  PM, Otis Gospodnetic wrote:

> Hello,
> Has anyone else observed this behaviour?
>
>> Wrong ordering from Document.fields()
>>
> http://sourceforge.net/tracker/index.php?func=detail&aid=451317&group_id=
> 3922&
>> atid=103922
>
> It looks like java.util.Enumeration is used to store the fields, so if
> Enumeration guarantees order than this should, too.
> Could you please provide a self-contained test case that I can just put
> somewhere, compile, and run?
> I can't compile the snippet in the above bug report.
>
>> No software is bug free; I just want to help make Lucene better.  If
>> I can be of any help, please ask.
>
> Thanks!

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to