You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by bib_lucene bib <bi...@yahoo.com> on 2005/07/10 06:40:35 UTC
Query -- how to write
Hi All
I am indexing a document like this...
Document doc = new Document();
doc.add(Field.Text("contents", new FileReader(f)));
doc.add(Field.Text("filename",f.getCanonicalPath()));
Iterator it = fields.keySet().iterator();
String element = "";
while (it.hasNext()) {
element = (String)it.next();
doc.add(Field.Text(element,(String)fields.get(element))); // also tried field.keyword
}
writer.addDocument(doc);
writer.optimize();
writer.close();
So the lucene doc has the text of the uploaded doc and also some params like author, date, etc.
When I write my search
String queryStr = request.getParameter("query");
File indexDir = new File("c:/luceneindex");
Directory fsDir = FSDirectory.getDirectory(indexDir,false);
IndexSearcher is = new IndexSearcher(fsDir);
Query query = QueryParser.parse(queryStr,"contents", new StandardAnalyzer());
Hits hits = is.search(query);
Not surprisingly I get a match only if there is a word in the contents of the document.
Question: If my query should be able to search not only document contents but also the other data added such as author how do I do that.
Please let me know.
Also if I can get a pointer this type of info I will really appreciate it.
Thanks
bib/-
---------------------------------
Yahoo! Mail
Stay connected, organized, and protected. Take the tour
Re: Query -- how to write
Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Jul 10, 2005, at 12:40 AM, bib_lucene bib wrote:
> Hi All
>
> I am indexing a document like this...
>
> Document doc = new Document();
> doc.add(Field.Text("contents", new FileReader(f)));
> doc.add(Field.Text("filename",f.getCanonicalPath()));
> Iterator it = fields.keySet().iterator();
> String element = "";
> while (it.hasNext()) {
> element = (String)it.next();
> doc.add(Field.Text(element,(String)fields.get
> (element))); // also tried field.keyword
> }
> writer.addDocument(doc);
> writer.optimize();
> writer.close();
>
> So the lucene doc has the text of the uploaded doc and also some
> params like author, date, etc.
>
> When I write my search
>
> String queryStr = request.getParameter("query");
>
> File indexDir = new File("c:/luceneindex");
>
> Directory fsDir = FSDirectory.getDirectory(indexDir,false);
>
> IndexSearcher is = new IndexSearcher(fsDir);
>
> Query query = QueryParser.parse(queryStr,"contents", new
> StandardAnalyzer());
>
> Hits hits = is.search(query);
>
> Not surprisingly I get a match only if there is a word in the
> contents of the document.
>
> Question: If my query should be able to search not only document
> contents but also the other data added such as author how do I do
> that.
There are two common options here:
- MultiFieldQueryParser
- Use an aggregate field for all searchable text
I personally prefer the aggregate field approach as it makes the
queries much simpler under the covers.
Erik
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org