lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spinergywmy <spinergy...@gmail.com>
Subject Re: Filter query method
Date Fri, 10 Nov 2006 09:47:47 GMT

Hi Doron,

   I m not sure I m implement your suggestion correctly.

   The way I did is I have 2 separate methods controlling by the check box.
I used basic search method for the first time and that will look up the
index from the directory. After I got the result, I will check the checkbox
and that will lead to 2nd method which the reader again from the index
directory and searcher search based on the reader. Next, I  m only implement
the codes that suggested by you. So, I m not sure I m doing the right thing
or not.

   I have attached the work that I done on below:

      public String search(String searchString) throws IOException,
Exception 
	{
		StringBuffer buff = new StringBuffer();
		String field = "";
		
		try
		{
			reader = IndexReader.open(DsConstant.indexDir);
			Searcher searcher = new IndexSearcher(reader);
			Analyzer analyzer = new StandardAnalyzer();
			QueryParser parser = new QueryParser(DsConstant.idxFileContent,
analyzer);
			//Query query = parser.parse(searchString);
			
			String[] metaFields = field.split(":");
			metaFields = new String[]{"contents", "companyId", "ownerId",
"createdBy", "docName",   "docDesc", "keywords", "docId", "docPropName"};
			
			for(int i = 0; i < metaFields.length; i++)
			{
				buff.append(metaFields[i] + ":" + searchString);
				if(i != (metaFields.length-1))
				{
					buff.append(" OR ");
				}
			}
			
			Query query = parser.parse(buff.toString());
			query = query.rewrite(reader);
			
			System.out.println("query ::: " + query);
			
		    searchHits = searcher.search(query);
		    
		    System.out.println("search hits is ::: " +searchHits.length());
			
			if(searchHits.length() > 0)
			{
				QueryScorer scorer = new QueryScorer(query);
				Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span
style='background-color:yellow;  font-weight:bold;'>",
								"</span>"), scorer);
				
				for(int i = 0; i < searchHits.length(); i++)
				{
					Document doc = searchHits.doc(i);
					String text = doc.get(DsConstant.idxFileContent);
					TokenStream tokenstream =
analyzer.tokenStream(DsConstant.idxFileContent, new StringReader(text));
					//buff.append("<p> '" + DsConstant.userDir
					buff.append("!");
					buff.append("<p  " + searchHits.doc(i).get(DsConstant.idxPath) + " "
							+ searchHits.doc(i).get("docName") + " <br>");
					buff.append("score: " + searchHits.score(i) + "<br>");
					buff.append(highlighter.getBestFragments(tokenstream, text, 3, "...")+
"</p>");
				}
				
				//System.out.println("Folder path is ::: " +DsConstant.folderPath);
				
				searcher.close();
			}
			
			System.out.println("Found "+searchHits.length()+" searchHits with query =
"+query);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return buff.toString();
		//return resultList;
	}
	
	public String refindSearchResult(String searchString1, String
searchString2) throws IOException, Exception 
	{
		System.out.println("inside search util - refind search result");
		
		IndexReader reader = null;
		StringBuffer buff = new StringBuffer();
		
		try
		{
			reader = IndexReader.open(DsConstant.indexDir);
			Searcher searcher = new IndexSearcher(reader);
			Analyzer analyzer = new StandardAnalyzer();
			QueryParser parser = new QueryParser(DsConstant.idxFileContent,
analyzer);
			
			Query query1 = parser.parse(searchString1);
			filter = new QueryFilter(query1);
			Query query2 = parser.parse(searchString2);
			
		    searchHits = searcher.search(query2, filter);
			
			System.out.println("search hits is ::: " +searchHits.length());
			
			if(searchHits.length() > 0)
			{
				QueryScorer scorer = new QueryScorer(query2);
				Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span
style='background-color:yellow;  font-weight:bold;'>",
								"</span>"), scorer);
				
				for(int i = 0; i < searchHits.length(); i++)
				{
					Document doc = searchHits.doc(i);
					String text = doc.get(DsConstant.idxFileContent);
					TokenStream tokenstream =
analyzer.tokenStream(DsConstant.idxFileContent, new StringReader(text));
					//buff.append("<p> '" + DsConstant.userDir
					buff.append("<p  " + searchHits.doc(i).get(DsConstant.idxPath) + " "
							+ searchHits.doc(i).get("docName") + " <br>");
					buff.append("score: " + searchHits.score(i) + "<br>");
					buff.append(highlighter.getBestFragments(tokenstream, text, 3, "...")+
"</p>");
					buff.append("!").substring(i);
				}
				
				searcher.close();
			}
			
			System.out.println("Found "+searchHits.length()+" searchHits with query =
"+query2);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return buff.toString();
	}

   Please point out where I m done wrong and hopefully the right solution
that I can use.

   Thank you.

regards,
Wooi Meng
-- 
View this message in context: http://www.nabble.com/Filter-query-method-tf2586547.html#a7273997
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
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