lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Aleksander M. Stensby" <aleksander.sten...@integrasco.no>
Subject Re: question
Date Fri, 30 Jun 2006 15:22:29 GMT
Dammit, i just wrote a long mail, and opera found out that it should just  
delete it before i got to send it:( grrrrr..

well,

A short version. You are misunderstanding the field functions I think.

final Document doc = new Document();
doc.add(new Field("resume_name", resume_name, Store.YES,  
Index.UN_TOKENIZED));
doc.add(new Field("details", details, Store.YES, Index.UN_TOKENIZED));
writer.addDocument(doc);

Stores a document with TWO fields, one is called "resume_name", with the  
value of your variable resume_name, and the other field is called  
"details", with the value of your variable named details.

You should consider storing these documents in the index using  
Index.TOKENIZED. Please check the API.

The problem lies in the searching.
:

I would do it like this: (im not catching any exceptions, thats up to  
you!):

	Analyzer analyzer = new StandardAnalyzer();
	//the first argument is the fieldname to search in, in your case either  
"resume_name" or "details".
	QueryParser parser = new QueryParser("details", analyzer);

	//this takes the indexpath as an argument, in your case it is "index"
	IndexSearcher searcher = new IndexSearcher("index");

	String querystring = "whats new pussycat";
	Query query = parser.parse(querystring);
	Hits hits = searcher.search(query);

//Instead of Hits, you can use HitsCollector, see the wiki or the API.
You now have the hits object, wich you can use to iterate over your  
results and access them in this way:

	Document doc = hits.doc(i); // where i is the iterator

THen you can access your stored fields by doing this:

	String resume_name = doc.get("resume_name");
	String details = doc.get("details");

Hopefully this helps you a bit.


On Fri, 30 Jun 2006 15:39:24 +0200, <amit_kkumar@persistent.co.in> wrote:

> hi,
>
> i am able to index the database but while searching
> it does'nt
> show any result
>
> i am sending u part of my code just chk and tell me
> where is error
>
> code---
>
> class DBReader
> {
> static final File INDEX_DIR = new File("index");
> public static void main(String args[])
> {
> try {
> // The newInstance() call is a work
> around for some
> // broken Java implementations
>
> Class.forName("com.mysql.jdbc.Driver").newInstance();
> System.out.println("driver loaded
> successfully");
> } catch (Exception ex) {
> System.out.println("DBError:"+ex);
> }
>
> try {
> Connection conn =
> DriverManager.getConnection("jdbc:mysql://localhost/experience?user=root&password=");
> System.out.println("connection success");
>
> // Do something with the Connection ....
> Statement stmt = conn.createStatement();
> String select_query = "SELECT
> resume_name,years,months,details FROM info";
> ResultSet rs =
> stmt.executeQuery(select_query);
>
> IndexWriter writer = new
> IndexWriter(INDEX_DIR, new StandardAnalyzer(),
> true);
>
> while (rs.next()) {
> String resume_name =
> rs.getString("resume_name");
> float years = rs.getFloat("years");
> float months = rs.getFloat("months");
> String details =
> rs.getString("details");
>
> //System.out.println(resume_name+"\t"+years+"\t"+months+"\t"+details);
> //try {
> //Thread.sleep(1000);
> //}
> //catch(InterruptedException e) {
> // probably fine to ignore
> this exception
> //}
> writer.setMaxFieldLength(25000);
>
> final Document doc = new Document();
> doc.add(new Field("resume_name",
> resume_name, Store.YES, Index.UN_TOKENIZED));
> doc.add(new Field("details", details,
> Store.YES, Index.UN_TOKENIZED));
> writer.addDocument(doc);
> }
> writer.optimize();
> writer.close();
>
> IndexReader reader =
> IndexReader.open("index");
> Searcher searcher = new
> IndexSearcher(reader);
> Analyzer analyzer = new
> StandardAnalyzer();
> BufferedReader in = new
> BufferedReader(new InputStreamReader(System.in,
> "UTF-8"));
> String field = "contents";
> QueryParser parser = new
> QueryParser(field, analyzer);
>
> while (true)
> {
> System.out.print("Query: ");
>
> String line = in.readLine();
>
> if (line == null ||
> line.length() == -1)
> break;
>
> Query query = parser.parse(line);
> System.out.println("Searching
> for: " + query.toString(field));
>
> Hits hits =
> searcher.search(query);
>
> System.out.println(hits.length() + " total matching
> documents");
>
> final int HITS_PER_PAGE = 10;
> for (int start = 0; start <
> hits.length(); start += HITS_PER_PAGE)
> {
> int end =
> Math.min(hits.length(), start + HITS_PER_PAGE);
> for (int i = start; i
> < end; i++)
> {
> Document docs
> = hits.doc(i);
> String path =
> docs.get("path");
> if (path != null)
> {
>
> System.out.println((i+1) + ". " + path);
> String
> title = docs.get("title");
> if
> (title != null)
> {
>
> System.out.println(" Title: " + docs.get("title"));
> }
> }else{
>
> System.out.println((i+1) + ". " + "No path for this
> document");
> }
> }
>
> if (hits.length() > end)
> {
>
> System.out.print("more (y/n) ? ");
> line =
> in.readLine();
> if
> (line.length() == 0 || line.charAt(0) == 'n')
> break;
> }
> }//end of for(int start = 0;
> start < hits.length(); start += HITS_PER_PAGE)
> }//end of while(true)
>
> } catch (SQLException ex) {
> // handle any errors
> System.out.println("SQLException: " +
> ex.getMessage());
> System.out.println("SQLState: " +
> ex.getSQLState());
> System.out.println("VendorError: " +
> ex.getErrorCode());
> }
> catch(IOException ex)
> {
> }
> catch(Exception e)
> {
> }
> }
> }
>
> DISCLAIMER
> ==========
> This e-mail may contain privileged and confidential information which is  
> the property of Persistent Systems Pvt. Ltd. It is intended only for the  
> use of the individual or entity to which it is addressed. If you are not  
> the intended recipient, you are not authorized to read, retain, copy,  
> print, distribute or use this message. If you have received this  
> communication in error, please notify the sender and delete all copies  
> of this message. Persistent Systems Pvt. Ltd. does not accept any  
> liability for virus infected mails.



-- 
Aleksander M. Stensby
Software Developer
Integrasco A/S
aleksander.stensby@integrasco.no
Tlf.: +47 41 22 82 72

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


Mime
View raw message