lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Roy Klein" <kl...@sitescape.com>
Subject RE: Indexing multiple instances of the same field for each document
Date Fri, 27 Feb 2004 17:12:52 GMT
Doug,

The query results are different, I'm attaching my test code.

>> Also FYI, I found that phrase queries don't work against a field 
>> that's been added multiple times. If I query the phrase "brown fox", 
>> against the two example docs above, only the second matches.

>They should work the same.  I'm not sure what Field.indexed does. 
>That's not a normal Lucene method.

>Doug

Here's my test code  (my results follow it):
------------------------------------------------------------------------
---

package lucenetest;


import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.index.Term;



import java.io.File;
import java.io.IOException;
import java.io.FileReader;

public class test {
    public static void test(File indexDir) throws IOException {

        IndexWriter writer = new IndexWriter(indexDir, new
SimpleAnalyzer(), true);
        //indexDirectory(writer, dataDir);

        Document doc = new Document();
        doc.add(Field.Text("contents", "the"));
        doc.add(Field.Text("contents", "quick"));
        doc.add(Field.Text("contents", "brown"));
        doc.add(Field.Text("contents", "fox"));
        doc.add(Field.Text("contents", "jumped"));
        doc.add(Field.Text("contents", "over"));
        doc.add(Field.Text("contents", "the"));
        doc.add(Field.Text("contents", "lazy"));
        doc.add(Field.Text("contents", "dogs"));
        doc.add(Field.Keyword("docnumber", "1"));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(Field.Text("contents", "the quick brown fox jumped over
the lazy dogs"));
        doc.add(Field.Keyword("docnumber", "2"));
        writer.addDocument(doc);
        writer.close();
    }

    public static void query(File indexDir) throws IOException
    {
        Query query = null;
        PhraseQuery pquery = new PhraseQuery();
        Hits hits = null;

        try {
            query = QueryParser.parse("quick brown", "contents", new
StandardAnalyzer());
        } catch (Exception qe) {System.out.println(qe.toString());}
        if (query == null) return;
        System.out.println("Query: " + query.toString());
        IndexReader reader = IndexReader.open(indexDir);
        IndexSearcher searcher = new IndexSearcher(reader);

        hits = searcher.search(query);
        System.out.println("Hits: " + hits.length());

        for (int i = 0; i < hits.length(); i++)
        {
            System.out.println( hits.doc(i).get("docnumber") + " ");
        }


        pquery.add(new Term("contents", "quick"));
        pquery.add(new Term("contents", "brown"));
        System.out.println("PQuery: " + pquery.toString());
        hits = searcher.search(pquery);
        System.out.println("Phrase Hits: " + hits.length());
        for (int i = 0; i < hits.length(); i++)
        {
            System.out.println( hits.doc(i).get("docnumber") + " ");
        }

        searcher.close();
        reader.close();

    }
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            throw new Exception("Usage: " + test.class.getName() + "
<index dir>");
        }
        File indexDir = new File(args[0]);
        test(indexDir);
        query(indexDir);
    }
}

------------------------------------------------------------------------
-------
My results:
Query: contents:quick contents:brown 
Hits: 2 
1  
2  
PQuery: 
contents:"quick brown" 
Phrase Hits: 1 
2 


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message