lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doron Cohen <DOR...@il.ibm.com>
Subject Re: Filter query method
Date Fri, 10 Nov 2006 20:47:30 GMT
You did not specify what's wrong - in what way is the code below not
working as you expect?

Two things to check:

(1) search() and refindSearchResult() process the text of the first query
differently. In search() the text is added to multiple fields
("metaField"). The way it is done btw would not work well if searchString
is multi-word. You should take a look at the printout for the result query
to see if this is what you expect. In refindSearchResult() the metaFields
logic is not done for q1. You should print q1 and print the filter to see
they match what you expect, and also compare to the query as printed in
search().

(2) reuse searcher for better performance - currently in every call to
search() and to refindSearchResult() a new searcher is opened. This would
hurt performance. Better to reuse a searchers between queries, and, from
time to time, reopen it to refresh with recent index changes. Many
discussions on this in the mailing list.

Hope this helps,
Doron

spinergywmy wrote:

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


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