On Thursday, November 6, 2003, at 07:53 PM, Caroline Jen wrote:
> Hi, let me see if I have got the idea. For example,
> if I want to search the database for articles written
> by Elizabeth Castro, we do what is shown below in
> Lucene:
It sounds like you're asking a lot of hypothetical questions without
actually trying things out with Lucene. It's quite approachable and
easy to do some Test Driven Learning with some very simple JUnit test
cases. If you're familiar with JUnit, this should be straightforward:
public class SearchingTest extends TestCase {
private Directory directory;
private String[] docs = {"I come from nowhere mostly.",
"I mostly come from space."};
private String[] keys = {"",
""};
protected void setUp() throws Exception {
directory = new RAMDirectory();
addDocuments(directory);
}
private void addDocuments(Directory directory) throws Exception {
IndexWriter writer = new IndexWriter(directory,
new SimpleAnalyzer(),
true);
for (int i = 0; i < docs.length; i++) {
Document doc = new Document();
doc.add(Field.Text("contents", docs[i]));
doc.add(Field.Keyword("url", keys[i]));
writer.addDocument(doc);
}
writer.close();
}
public void testTerm() throws Exception {
IndexSearcher searcher = new IndexSearcher(directory);
Term t = new Term("contents", "space");
Query query = new TermQuery(t);
Hits hits = searcher.search(query);
assertEquals(1, hits.length());
t = new Term("contents", "i");
hits = searcher.search(new TermQuery(t));
assertEquals(2, hits.length());
searcher.close();
}
}
Substitute in the QueryParser stuff where the TermQuery is constructed
and index some documents of your choosing in the docs field.
> Nonetheless, both "creator" and the name of the
> creator are variables. We depend on the user to give
> us the information and we pass the information to form
> a query; probably this way:
Whether to use QueryParser completely or not is a decision you'll have
to make, but using it for the entire Query may not be quite what you're
after.
> I think I want to put my hand on the idea on how to
> pass the information (two pairs of field and its
> value) provided by a user to form a query. Should I
> do this:
>
> public static final Query getQuery(
> String value1,
> String field1,
> String value2,
> String field2) throws ParameterException {
> try {
> return
> QueryParser.parse(value1,field1,value2,field2,
> getAnalyzer());
> }
Well, if you had tried this, you'd realize this is incorrect API usage,
so no, you shouldn't do this! :) (nor could you)
> return QueryParser.parse(value1,field1,value2,field2,
> getAnalyzer());
>
> How does the Luncene know it is AND instead of OR we
> want?
QueryParser has a setOperator method on it to change the default
operator to AND rather than OR.
Again, please have a go at implementing some examples and come back to
us with informed questions should you have some. Trying this stuff
first hand is the best way to really get a feel for how it works and
will ensure you're asking the right questions. Test Driven Learning -
embrace learning. It's my new fad.
Erik
---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org