Thank you so much for your patience and support but i am still not
getting the correct result. Here is my code can you please tell me
what wrong have i done in it? (I don't want to use
org.apache.search.hit so i have used terms in place of that)
package highlighted;
import java.io.FileWriter;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.spans.Spans;
import org.apache.lucene.store.Directory;
@SuppressWarnings({ "deprecation", "unused" })
public class testsearch {
public static String FIELD_NAME="text";
private Query query;
public Hits hits = null;
Analyzer analyzer= new StandardAnalyzer();
public void doSearching(String queryString,Directory dir) throws
Exception
{ QueryParser parser= new QueryParser(FIELD_NAME,analyzer);//new
StandardAnalyzer());
query=parser.parse(queryString);
IndexSearcher Searcher = new IndexSearcher(dir);
IndexReader reader = IndexReader.open(dir);
TermEnum te = reader.terms();
hits=Searcher.search(query);
while ( te.next() ) {
Term tt = te.term();
String fieldOfTerm = tt.field();
int dummy= fieldOfTerm.compareTo("text");
int dummy1= fieldOfTerm.compareTo("path");
boolean anscontents=false;
boolean anspath=false;
if (dummy==0)
anscontents=true;
if(dummy1==0)
anspath=true;
if (anscontents ) {
anscontents=false;
if(tt.text().equals(queryString))
{ System.out.println("Content field found--->>"+
tt.text());
doStandardHighlights(tt.text());
}
}
if ( anspath ) {anspath=false;
}
}
}
void doStandardHighlights(String text) throws Exception
{
Highlighter highlighter = new Highlighter(new
QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(200));
int maxNumFragmentsRequired=200;
String FragmentSeparator="...";
Fragmenter fragmenter = new SimpleFragmenter(50);
highlighter.setTextFragmenter(fragmenter);
TokenStream tokenStream=analyzer.tokenStream(FIELD_NAME,new
StringReader(text));
String
result=highlighter.getBestFragments(tokenStream,text,maxNumFragmentsRequired,FragmentSeparator);
System.out.println( "This is the result "+result);
FileWriter writer = new FileWriter("/root/Desktop/dummy");
writer.write("<html>");
writer.write("<script>\n" +
".highlight {\n" +
" background: yellow;\n" +
"}\n" +
"</style>\n"+ "</script>" );
writer.write(result);
writer.write("</body></html>");
writer.close();
}
}
And the main program i am using is as follows:
package highlighted;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class maintestsearch {
public static void main(String[] args) throws Exception {
Directory dir=FSDirectory.getDirectory("/root/indexed");
testsearch ob=new testsearch();
ob.doSearching("alaska",dir);
}
}
and the output it shows is :
Content field found--->>alaska
This is the result <B>alaska</B>
On 27/05/2009, KK <[email protected]> wrote:
> @Ritu
> Wouter's reply must have fixed the problem, right? Or still stuck?
>
> --KK
>
> On Wed, May 27, 2009 at 1:46 PM, Wouter Heijke <[email protected]> wrote:
>
>> Hi,
>> It sounds to me that you are highlighting the query string and not the
>> document. You will have to pass the document's content to
>> getBestFragments() and it will work I think.
>>
>> Wouter
>>
>> > hi there,
>> > I am using lucene highlighter to highlight the searched result
>> > but it shows only the query string in bold highlights.
>> > IS THERE ANY WAY I CAN USE IT TO SHOW THE HIGHLIGHTED TEXT IN THE
>> > DOCUMENT WHERE IT IS FOUND?
>> > I need to show the searched terms in highlights in the
>> > document where it is found and i want to do it without using
>> > org.apache.lucene.search.Hits
>> > Please help. Thanks in advance.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]