querying multiple fields

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

querying multiple fields

George Abraham
All,
Forgive me for the basic question. When you are querying multiple
fields using QueryParser, what is the exact code?

I tried QueryParser.parse(queryString, "SearchTerms", analyzer) where
queryString was "SearchTerms:visnu temple ImageExistsBit:1",
SearchTerms and ImageExistsBit being the two fields I want searched.
However it does not seem to do what I want it to do.

TIA,
George

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

Erik Hatcher

On Jun 22, 2005, at 10:49 AM, George Abraham wrote:

> All,
> Forgive me for the basic question. When you are querying multiple
> fields using QueryParser, what is the exact code?
>
> I tried QueryParser.parse(queryString, "SearchTerms", analyzer) where
> queryString was "SearchTerms:visnu temple ImageExistsBit:1",
> SearchTerms and ImageExistsBit being the two fields I want searched.
> However it does not seem to do what I want it to do.

What analyzer are you using?   What is the .toString of the generated  
Query instance?  How did you index those fields?

Without answering those, it'd be guess work to offer any help.

     Erik

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

Otis Gospodnetic-2
In reply to this post by George Abraham
George,

You can use MultiFieldQueryParser instead of QueryParser.

Otis


--- George Abraham <[hidden email]> wrote:

> All,
> Forgive me for the basic question. When you are querying multiple
> fields using QueryParser, what is the exact code?
>
> I tried QueryParser.parse(queryString, "SearchTerms", analyzer) where
> queryString was "SearchTerms:visnu temple ImageExistsBit:1",
> SearchTerms and ImageExistsBit being the two fields I want searched.
> However it does not seem to do what I want it to do.
>
> TIA,
> George
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

George Abraham
Otis,
I think MultiFieldQueryParser (if I am not mistaken) uses the same
query string to search multiple fields. Let me know if it is
otherwise.

Erik,
Let me see if I can answer those questions. Here are some code
snippets, by the way.

FOR INDEX
IndexWriter writer = new IndexWriter(indexDir, new StopAnalyzer(), true);
while (rs.next()){ //rs is a database resultset I am looping over
  Document doc = new Document();
  doc.add(Field.Keyword("ObjectID", ObjectID));
  doc.add(Field.Keyword("ImageExistsBit",
ImageExistsBit));//ImageExistsBit is 1 or 0
  doc.add(Field.Text("SearchTerms", SearchTerms));
  writer.addDocument(doc);
  writer.optimize();
  writer.close();
}

FOR SEARCH
searcher = new IndexSearcher(IndexReader.open(indexPath) );
Analyzer analyzer = new StopAnalyzer();
luceneQuery = QueryParser.parse(queryString, "SearchTerms", analyzer);
hits = searcher.search(luceneQuery);
System.out.println("Found " + hits.length() + " document(s).");

What I want: I want all the ObjectIDs that have the term 'visnu
temple' in the SearchTerms field and has ImageExistsBit=1. So the
queryString above is: SearchTerms:\"visnu temple\" ImageExistsBit:1".
The .toString() for this query is SearchTerms:"visnu temple"

Thanks,
George



On 6/22/05, Otis Gospodnetic <[hidden email]> wrote:

> George,
>
> You can use MultiFieldQueryParser instead of QueryParser.
>
> Otis
>
>
> --- George Abraham <[hidden email]> wrote:
>
> > All,
> > Forgive me for the basic question. When you are querying multiple
> > fields using QueryParser, what is the exact code?
> >
> > I tried QueryParser.parse(queryString, "SearchTerms", analyzer) where
> > queryString was "SearchTerms:visnu temple ImageExistsBit:1",
> > SearchTerms and ImageExistsBit being the two fields I want searched.
> > However it does not seem to do what I want it to do.
> >
> > TIA,
> > George
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

Erik Hatcher
On Jun 22, 2005, at 1:24 PM, George Abraham wrote:

> Otis,
> I think MultiFieldQueryParser (if I am not mistaken) uses the same
> query string to search multiple fields. Let me know if it is
> otherwise.
>
> Erik,
> Let me see if I can answer those questions. Here are some code
> snippets, by the way.
>
> FOR INDEX
> IndexWriter writer = new IndexWriter(indexDir, new StopAnalyzer(),  
> true);
> while (rs.next()){ //rs is a database resultset I am looping over
>   Document doc = new Document();
>   doc.add(Field.Keyword("ObjectID", ObjectID));
>   doc.add(Field.Keyword("ImageExistsBit",
> ImageExistsBit));//ImageExistsBit is 1 or 0
>   doc.add(Field.Text("SearchTerms", SearchTerms));
>   writer.addDocument(doc);
>   writer.optimize();
>   writer.close();
> }
>
> FOR SEARCH
> searcher = new IndexSearcher(IndexReader.open(indexPath) );
> Analyzer analyzer = new StopAnalyzer();
> luceneQuery = QueryParser.parse(queryString, "SearchTerms", analyzer);
> hits = searcher.search(luceneQuery);
> System.out.println("Found " + hits.length() + " document(s).");
>
> What I want: I want all the ObjectIDs that have the term 'visnu
> temple' in the SearchTerms field and has ImageExistsBit=1. So the
> queryString above is: SearchTerms:\"visnu temple\" ImageExistsBit:1".
> The .toString() for this query is SearchTerms:"visnu temple"

Your analyzer is eating the ImageExistsBit:1 because "1" returns no  
tokens through StopAnalyzer.  Here's a solution, adapted from the  
code that powers lucenebook.com.  Use this as your analyzer:

       PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper
(new StopAnalyzer());
       analyzer.addAnalyzer("ImageExistsBit", new WhitespaceAnalyzer());

That should do the trick.

     Erik




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

George Abraham
Erik,
That worked like a charm. It seems to me that reading up on the
different analyzers would solve a lot of questions in my mind.
Unfortunately your book has not arrived yet from Amazon. Aarrgh!

Thanks a ton!
George

> Your analyzer is eating the ImageExistsBit:1 because "1" returns no
> tokens through StopAnalyzer.  Here's a solution, adapted from the
> code that powers lucenebook.com.  Use this as your analyzer:
>
>       PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper
> (new StopAnalyzer());
>       analyzer.addAnalyzer("ImageExistsBit", new WhitespaceAnalyzer());
>
> That should do the trick.
>
>     Erik

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: querying multiple fields

Erik Hatcher

On Jun 22, 2005, at 2:02 PM, George Abraham wrote:

> Erik,
> That worked like a charm. It seems to me that reading up on the
> different analyzers would solve a lot of questions in my mind.

It would solve a lot of folks questions!  I find the analysis part of  
Lucene fascinating.  The really interesting thing is that its  
actually quite simple, yet packs a lot of power.  What makes a word a  
"word" (or term, in the Lucene sense) - that is quite a fun  
rhetorical question to ask.  This is why I strongly advocate the use  
of the AnalyzerDemo from the Lucene in Action source code, or some  
variant thereof (like Luke's capability to do the same sort of  
thing).  Visually seeing what comes out of the analysis process for  
sample strings is really eye opening.

> Unfortunately your book has not arrived yet from Amazon. Aarrgh!

Wow, they're slow... 'cause I know you ordered it back in December  
when it was released :))

Don't forget that you can get the book from Manning (www.manning.com)  
in e-book form and have the hard-copy of it as well.

     Erik



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]