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 spinergywmy <sp...@gmail.com> on 2006/11/02 08:43:04 UTC

search within search

Hi,

   I want to perform a search within search feature in my application, so I
having this problem and stuck at this point. I  be able to retrieve search
index from my first search, but having problem to search within the result
that I retrieved. I have gone through some of the mailing list archieves,
and mentined to use queryFilter method and bitSet method, however I m still
not quite get it. In fact, I construct the index search to return in String
by using Hits, and I noticed from the archieves it will be a problem doing
with Hits in terms of performance wise.

   However, I hope there are someone can provide me examples on how to solve
this problem with not changing a lot the way I coded.

   Please let me know if the problem above not clear to you and I will
explain again.

   Thanks.

regards,
Wooi Meng
-- 
View this message in context: http://www.nabble.com/search-within-search-tf2558237.html#a7129070
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


Re: search within search

Posted by Erick Erickson <er...@gmail.com>.
I *strongly* recommend that you get a copy of "Lucene in Action". It has
many examples and I found it extremely helpful when I started out....

Best
Erick

On 11/2/06, spinergywmy <sp...@gmail.com> wrote:
>
>
> Hi,
>
>    Thanks Erik.
>
>    Is there any example that I can refer to cause I m actually quite new
> to
> apache lucene although it is sometimes quite straight forward, but I hope
> u
> understand and it is better to have a guide for me.
>
>    Thanks
>
>
> regards,
> Wooi Meng
> --
> View this message in context:
> http://www.nabble.com/search-within-search-tf2558237.html#a7134849
> 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
>
>

Re: search within search

Posted by spinergywmy <sp...@gmail.com>.
Hi,

   Thanks Erik.

   Is there any example that I can refer to cause I m actually quite new to
apache lucene although it is sometimes quite straight forward, but I hope u
understand and it is better to have a guide for me.

   Thanks


regards,
Wooi Meng
-- 
View this message in context: http://www.nabble.com/search-within-search-tf2558237.html#a7134849
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


Re: search within search

Posted by spinergywmy <sp...@gmail.com>.
Hi,

   Doron, thanks for the advice.

regards,
Wooi Meng
-- 
View this message in context: http://www.nabble.com/search-within-search-tf2558237.html#a7171019
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


Re: search within search

Posted by Doron Cohen <DO...@il.ibm.com>.
spinergywmy <sp...@gmail.com> wrote on 03/11/2006 00:40:42:

>    I have another problem is I do not perform the real search within
search
> feature which according to the way that I have coded, because for the
second
> time searching, I actually go back to the index directory to search the
> entire indeces again rather then cached the first time search result.
>
>    How can I solve this problem? Do I need to use queryFilter and
> reconstruct the codes again, and that is time consuming, is there any how
I
> can get it done without reconstruct. Or do I need to use bitSet within my
> existing codes.

This was the recommendation you got on this in the list (forgot who it
was): submit query1 ANDed with query2. True, this is searching again the
"entire" index. In particular, it is re-doing work already done for query1.
However this is the simplest approach, with equivalent results. Unless you
are facing performance problems this should be sufficient.

If however, you are facing performance issues, say, if the queries are very
large, and the index is large as well, and you have more than 2 stages
(search within (search within (search within search (...)))), and
resubmitting a larger and larger boolean query is out of the question - you
can go with the filter approach. For this you can use your hit collector,
which, while query1 is processed, would populate a bitset to be used for a
filter for query2, should that be requested by the user. But I wouldn't go
there if I don't have to.

Doron



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


Re: search within search

Posted by spinergywmy <sp...@gmail.com>.
Hi,

   Doron, good call, thanks.

   I have another problem is I do not perform the real search within search
feature which according to the way that I have coded, because for the second
time searching, I actually go back to the index directory to search the
entire indeces again rather then cached the first time search result.

   How can I solve this problem? Do I need to use queryFilter and
reconstruct the codes again, and that is time consuming, is there any how I
can get it done without reconstruct. Or do I need to use bitSet within my
existing codes.

   Thanks.


regards,
Wooi Meng
-- 
View this message in context: http://www.nabble.com/search-within-search-tf2558237.html#a7153721
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


Re: search within search

Posted by Doron Cohen <DO...@il.ibm.com>.
This code adds the same query twice to a boolean query:
                                     Query query =
parser.parse(searchString);
                                     bq1.add(query,
BooleanClause.Occur.MUST);
                                     bq1.add(new BooleanClause(query,
BooleanClause.Occur.MUST));
But  X AND X == X  :-)

You probably want to do something like:
      Query q = new BooleanQuery();
      bq.add(parser.parse(searchString1), BooleanClause.Occur.MUST);
      bq.add(parser.parse(searchString2), BooleanClause.Occur.MUST);
      ...
      searchHits = searcher.search(q);

Regards,
Doron

spinergywmy <sp...@gmail.com> wrote on 02/11/2006 22:13:05:

>
> Hi,
>
>    I have look at the examples from lucene source, and try out myself but
it
> doesn't work. Perhaps u can point out where I did wrong. Below r the
codes
> that I developed:
>
>    public String search(String searchString) throws IOException,
Exception
>    {
>       //System.out.println("inside search util");
>
>       IndexReader reader = null;
>       StringBuffer buff = new StringBuffer();
>       BooleanQuery bq1 = new BooleanQuery();
>       //BooleanQuery bq2 = new BooleanQuery();
>       //ArrayList resultList = new ArrayList();
>
>       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);
>
>          bq1.add(query, BooleanClause.Occur.MUST);
>          bq1.add(new BooleanClause(query, BooleanClause.Occur.MUST));
>          //bq2.add(query, BooleanClause.Occur.MUST);
>          //bq1.add(bq2, BooleanClause.Occur.MUST);
>
>          searchHits = searcher.search(bq1);
>
>          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("<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("!");
>             }
>
>             //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;
>    }
>
>    Thanks.
>
> regards,
> Wooi Meng
>
> --
> View this message in context: http://www.nabble.com/search-within-
> search-tf2558237.html#a7152393
> 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


Re: search within search

Posted by spinergywmy <sp...@gmail.com>.
Hi,

   I have look at the examples from lucene source, and try out myself but it
doesn't work. Perhaps u can point out where I did wrong. Below r the codes
that I developed:

   public String search(String searchString) throws IOException, Exception 
	{
		//System.out.println("inside search util");
		
		IndexReader reader = null;
		StringBuffer buff = new StringBuffer();
		BooleanQuery bq1 = new BooleanQuery();
		//BooleanQuery bq2 = new BooleanQuery();
		//ArrayList resultList = new ArrayList();
		
		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);
			
			bq1.add(query, BooleanClause.Occur.MUST);
			bq1.add(new BooleanClause(query, BooleanClause.Occur.MUST));
			//bq2.add(query, BooleanClause.Occur.MUST);
			//bq1.add(bq2, BooleanClause.Occur.MUST);
			
			searchHits = searcher.search(bq1);
			
			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("<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("!");
				}
				
				//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;
	}   

   Thanks.

regards,
Wooi Meng

-- 
View this message in context: http://www.nabble.com/search-within-search-tf2558237.html#a7152393
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