Skip to main content

How to use your own search engine

No replies

A question has been asked on how to integrate your own search engine
into JavaHelp. Below is correspondence with user that has successfully
implemented the lucene search engine as an alternative search engine.


>My problem is how to integer
>> javahelp and lucene search engine,that can solve the problem of
>> chinese language searching .

To use an alternative search engine you will need to implement/extend
the APIs.

Start by extending You only need to
extend the (URL, Hashtable) constructor. Calling super will load "base"
and "param" field. "base" is the base address of the HelpSet. "params"
are the parameters specified in the the search view in the helpset.
You'll have to decide how this information will be relevant to your own
search engine.

Next implement the createQuery abstract method. This is a very simple
class that will return a SearchQuery which you are going to extend from Most likely you'll need to pass the
SearchEngine as a parameter to the SearchQuery constructor otherwise you
will have no access to where the search data resides. At this point
you're done with the extended SearchEngine and you can proceed to

In extending there is little need to
implement the (SearchEngine) constructor. We do in the default but only
for internal reasons. All the listener code works fine on it's own so
you don't have to extend it. You will need to extend the start method.
In the default we check to make sure the search isn't active before
starting the search. Also before you start the search call super.start
so that listener will fire a start search event. In the default we do
the search in a separate thread. This is optional and for your initial
code I would advise against it. You also need to implement isActive and
return a boolean indicating if the search is currently active.

While process your search if you find any items that match create a for every item and store the items in a
java.util.Vector. When you have all the events call your implementation
of Pass a "true" and the vector of
hits. When the search is finished call the searchQuery.fireSearchFinished().

That's it. It isn't rocket science and is fairly easily to implement if
you have java based search engine to work with. The exercise for you to
determine is how to integrate the lucene search engine into this
process. That's beyond the scope of the help that I can provide.



> In the light of your method, I have finished my work. I feel very
excited about it.
> The only problem is that I can't find the SearchItem's "begin" and
"end" pointer.
> public class SearchItem {
> private URL base;
> private String title;
> private String lang;
> private String filename;
> private double confidence;
> private int begin;
> private int end;
> private Vector concepts;
> }
> This is part of my program
> SearchItem si = new SearchItem(myurl, title,
> Locale.getDefault().toString(),
> filename,
> hits.score(i),
> 0,
> 0,
> new Vector(1));
> "concepts" is empty vector. because I don't known the meaning of it.
> the field of "begin" and "end" is 0. because I cannot find the
searchitem's begin and end position
> I have use the following code.
> the principle of this algorithm is :
> suppose: variable "word" is to be searched.
> firstly, to get the html file's content and save it into the
variable "content"
> secondly, to search everyword like "word" in "content" and get the
begin and end position of "word".
> thirdly, to pass the begin and end position to searchitem.
> public static void addSearchItem(File file,
> String title,
> String locale,
> String filename,
> float score,
> Vector concepts) {
> StringBuffer txt = null;
> try {
> txt = new StringBuffer();
> BufferedReader infile =
> new BufferedReader(
> new FileReader(file));
> String s = null;
> while ( (s = infile.readLine()) != null) {
> txt.append(s);
> }
> infile.close();
> }catch(Exception es){
> es.printStackTrace();
> return;
> }
> String content = txt.toString().toLowerCase();
> if(content == null)
> return;
> int dd = content.indexOf("");
> int index = content.indexOf(word, dd);// word is the searched word.
> int start = 0, end = 0;
> while(index >= 0 && index < content.length()){
> start = index;
> end = start + word.length();
> try {
> SearchItem si = new SearchItem(file.toURL(),
> title,
> locale,
> filename,
> score,
> start,
> end,
> concepts);
> result.add(si);
> }catch(Exception es){}
> index = content.indexOf(word, end);
> }
> }
> when I do that, I found the "begin" and "end" position of this method
isn't the exact "begin" and "end" position of searchitem
> What should I do?

Concepts is a vector of Strings that the match occurred on. Some search
engines have the capability of morphing words (click, clicking, clicked)
or compatible words (car, auto, automobile).

As far as the begin and end are concerned they are tightly tied to the
jeditor pane. They represent the place where the highlighting will
appear. Since your search engine isn't based on the jeditor pane make
them both zero ("0"). That should take care of the problem.



You may leave the list at any time by sending message with no subject and
are available at
Submit bug reports at