Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Lucene: Java-User

Attribute Search Bug

 

 

Lucene java-user RSS feed   Index | Next | Previous | View Threaded


cnew at fuse

Nov 28, 2001, 10:28 PM

Post #1 of 2 (248 views)
Permalink
Attribute Search Bug

This program illustrates what may be a bug. It creates an index, a document
with two fields. The second field is the problem. I use the Field
constructor to make a field that is not stored, is indexed, not tokenized
(there is no factory method for this combination).

The program then queries on the second field and no hits are returned.

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.QueryParser;

public class Bug1 {
public static void main(String[] args) throws Exception {
String indexPath = "./bugdex";
String fieldName = "catno";
String fieldValue= "AB12-42 2X";
IndexWriter writer;

// An index is created by opening an IndexWriter with the
// create argument set to true.
writer = new IndexWriter(indexPath, null, true);
writer.close();

// now index a document
try {
writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);

Document doc = new Document();
// add id as unindexed - can't search, but it is stored
doc.add(Field.UnIndexed("id", "12345"));
doc.add(new Field(fieldName,fieldValue,false,true,false));
writer.addDocument(doc);
writer.close();

} catch (Exception e) {
e.printStackTrace();
}

// now make a query and search for document
Searcher searcher = new IndexSearcher(indexPath);
Query query = QueryParser.parse(fieldValue, fieldName,
new SimpleAnalyzer());
Hits hits = searcher.search(query);
for (int i=0; i<hits.length(); i++) {
System.out.println(
"id "+hits.doc(i).get("id") + "; Score: " + hits.score(i));
}
System.out.println("\nNumber of hits: "+ hits.length());
}
}



----- Original Message -----
From: Doug Cutting <DCutting [at] grandcentral>
To: 'Lucene Users List' <lucene-user [at] jakarta>
Sent: Monday, November 26, 2001 2:42 PM
Subject: RE: Attribute Search


> > From: New, Cecil (GEAE) [mailto:cecil.new [at] ae]
> >
> > this is exactly what I was doing. Store=false, index=true,
> > and token=false.
> >
> > It appeared to work ok, but searches *never* returned any hits.
> >
> > That's why I suspect it is a bug.
>
> If you think this is a bug, please submit a test case, as a simple class
> whose 'main()' method illustrates the problem.
>
> Doug
>
> --
> To unsubscribe, e-mail:
<mailto:lucene-user-unsubscribe [at] jakarta>
> For additional commands, e-mail:
<mailto:lucene-user-help [at] jakarta>
>



--
To unsubscribe, e-mail: <mailto:lucene-user-unsubscribe [at] jakarta>
For additional commands, e-mail: <mailto:lucene-user-help [at] jakarta>


DCutting at grandcentral

Nov 29, 2001, 10:20 AM

Post #2 of 2 (246 views)
Permalink
RE: Attribute Search Bug [In reply to]

Thanks for the test case.

The problem is using SimpleAnalyzer when you parse your query.
SimpleAnalyzer uses LetterTokenizer, which turns your query for "AB12-42 2X"
into a two term query containing "ab" and "x". Since there are no instances
of the terms "ab" or "x" in your index, no documents are returned.

You either need to use a different Analyzer, one that knows that knows how
to tokenized the "catno" field, or don't use the query parser to construct
your query.

For example, if you instead construct your query as:
Query query = new TermQuery(new Term(fieldName, fieldValue));
then your document is found.

Note that you can combine such manually-constructed queries with queries
from the query parser by adding both to a BooleanQuery:
Query q2 = QueryParser.parse(....);
BooleanQuery finalQuery = new BooleanQuery();
finalQuery.add(query, true, false);
finalQuery.add(q2, true, false);
Hits hits = searcher.search(finalQuery);

Doug

> -----Original Message-----
> From: Cecil, Paula New [mailto:cnew [at] fuse]
> Sent: Wednesday, November 28, 2001 9:28 PM
> To: Lucene Users List
> Subject: Attribute Search Bug
>
>
> This program illustrates what may be a bug. It creates an
> index, a document
> with two fields. The second field is the problem. I use the Field
> constructor to make a field that is not stored, is indexed,
> not tokenized
> (there is no factory method for this combination).
>
> The program then queries on the second field and no hits are returned.
>
> import java.io.File;
> import java.io.IOException;
> import java.io.OutputStream;
> import java.util.*;
>
> import org.apache.lucene.analysis.SimpleAnalyzer;
> import org.apache.lucene.index.IndexWriter;
> import org.apache.lucene.document.*;
> import org.apache.lucene.search.*;
> import org.apache.lucene.queryParser.QueryParser;
>
> public class Bug1 {
> public static void main(String[] args) throws Exception {
> String indexPath = "./bugdex";
> String fieldName = "catno";
> String fieldValue= "AB12-42 2X";
> IndexWriter writer;
>
> // An index is created by opening an IndexWriter with the
> // create argument set to true.
> writer = new IndexWriter(indexPath, null, true);
> writer.close();
>
> // now index a document
> try {
> writer = new IndexWriter(indexPath, new
> SimpleAnalyzer(), false);
>
> Document doc = new Document();
> // add id as unindexed - can't search, but it is stored
> doc.add(Field.UnIndexed("id", "12345"));
> doc.add(new Field(fieldName,fieldValue,false,true,false));
> writer.addDocument(doc);
> writer.close();
>
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> // now make a query and search for document
> Searcher searcher = new IndexSearcher(indexPath);
> Query query = QueryParser.parse(fieldValue, fieldName,
> new SimpleAnalyzer());
> Hits hits = searcher.search(query);
> for (int i=0; i<hits.length(); i++) {
> System.out.println(
> "id "+hits.doc(i).get("id") + "; Score: " + hits.score(i));
> }
> System.out.println("\nNumber of hits: "+ hits.length());
> }
> }
>
>
>
> ----- Original Message -----
> From: Doug Cutting <DCutting [at] grandcentral>
> To: 'Lucene Users List' <lucene-user [at] jakarta>
> Sent: Monday, November 26, 2001 2:42 PM
> Subject: RE: Attribute Search
>
>
> > > From: New, Cecil (GEAE) [mailto:cecil.new [at] ae]
> > >
> > > this is exactly what I was doing. Store=false, index=true,
> > > and token=false.
> > >
> > > It appeared to work ok, but searches *never* returned any hits.
> > >
> > > That's why I suspect it is a bug.
> >
> > If you think this is a bug, please submit a test case, as a
> simple class
> > whose 'main()' method illustrates the problem.
> >
> > Doug
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:lucene-user-unsubscribe [at] jakarta>
> > For additional commands, e-mail:
> <mailto:lucene-user-help [at] jakarta>
> >
>
>
>
> --
> To unsubscribe, e-mail:
<mailto:lucene-user-unsubscribe [at] jakarta>
For additional commands, e-mail:
<mailto:lucene-user-help [at] jakarta>

--
To unsubscribe, e-mail: <mailto:lucene-user-unsubscribe [at] jakarta>
For additional commands, e-mail: <mailto:lucene-user-help [at] jakarta>

Lucene java-user RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.