Keyword Search in J2EE

Posted on 2001-06-23
Medium Priority
Last Modified: 2013-11-24
I'm looking for some guidelines/best practices for implementing a keyword search for products in J2EE.  I've referred to the Java Pet Store example, but unfortunately, the search they implemented is not designed according to the J2EE Blueprints (they even admit it in the comments).

Do I...
 1. Implement the search as an entity bean with a "findByKeywordSearch()" finder?  (I've done this and it works, but I'm not convinced that it's the best way).
 2. Implement the search as a stateless session bean returning value objects representing the results of the search?
 3. Implement the search the way that the JPS did and just settle with it being out of whack with the Blueprints?
 4. Some other way?

The points go to the person who provides the best solution with the best justification for their solution.  Special consideration given to solutions that come with references to articles and/or books that back up the solution.

Question by:mwalls

Accepted Solution

jpk041897 earned 1200 total points
ID: 6222321
Actualy, I don't think there is "best way" to perform a keyword search. At least not something that coud be made into a pattern. There are just to many variables.

First of, it depends on what DB engiene your running it on and what sort of packages/plugins it has. For instance, Oracle comes with the Contex cartidge which contains an API that helps locate records in text fieds or indexes in a quick and eficient manner. If such a tool is available in your DB, then a BMP Entity bean could be your best bet.

Additionaly, DB caching availability in you app. server might also influence your desition.

Another factor would be just how complex a search language you want to define. If your only foing to allow AND  operations then a CMP entity bean could work out just fine but if you are going to allow complex expretions with combinations of AND,OR and NOT; you would end up requiring both a session and an entity bean.

The type of sesion bean would depend on weather you want to support multiple subset retrieval of the result set or a single dump.

All these reasons (and probably some I haven't though of) probaby influenced the fact that the Finf in the JPS does not comnform to Blueprint specs.

Looking at your specifiv questions:

 1. Implement the search as an entity bean with a "findByKeywordSearch()" finder?  (I've done this and
it works, but I'm not convinced that it's the best way).

Its not necesarily the best way, no. It depends on how you have designed your DB. If you have an index set for each keyword then it could be (and at least for your app probably is) good enough.

On the other hand, for a very large set of data, the overhead caused by CMP entity beans could prove to be to expensive.

2. Implement the search as a stateless session bean returning value objects representing the results
of the search?

This option is certanly more flexible, and could work out prey well if using specialized tools such as ConTex. Of course, it breaks up the specs prety badly and does not support multiple result set subset retrieval.

Personaly, I would avoid this solution.

3. Implement the search the way that the JPS did and just settle with it being out of whack with the

This is probably a good bet (though I would not go about it in the manner that Sun wrote it). Suns solution works, but was written wit "works" in mind, not as an efficient solution.

4. Some other way?

If I were writing it, IMO I would go with a statefull session bean (called by a mechanism that could limit the staefulnes to, say a page context, such as a JSP or servlet). This bean would make calls to one or more CMP entity beans (One for each clasification of keyword) that return index enumerations. Once I had these, I would implement the complex query logic using a parser.

I would make the Entity bean(s) CMP to allow for portabity (even at the expense of efitiancy) but that probably stems from the fact that I'm lazy and prefer code resusabiliy.

Once I had the list of valid indices, I would make the actual retrievel from the DB (set up a getter method that would allow for multiple subset retrievel based on nuber of requested records). Just exactly how such a getter could be made more effitient would again depend on the specific DB being used since many DB's don't support subset retrievel in their SQL etentions.

I don't have any actual refs. or links but you might want to have a look at page 255 of Mastering Enterprise Java Beans by Ed Roman, which you can download from:


The book is a bit dated (covering th EJB 1.1 spec) but still prety good.

Hope this helps.

Author Comment

ID: 6225316
I am using Oracle with ConText/interMedia.  However, the specifics of how I performed the search (whether by using interMedia or by some other mechanism such as LIKE) I was going to abstract away in a DAO.

The big question for me was:  Who's going to use that DAO?  In my current prototype I have a BMP Entity Bean using the DAO in a findByKeywordSearch() finder.  It works fine, although I'm somewhat concerned (probably over-concerned) about the efficiency of entity beans, especially for what could potentially be a large result set.

My next thought was a stateless session bean.  A cursory glance at the pet store code showed that they implemented a search function as part of a stateless session bean.  Oddly enough, when I did a more detailed investigation of their code I discovered that they didn't use that session bean anywhere.

Instead, the actual search is done in a plain-vanilla JavaBean that has application scope.  (Note:  The stateless session bean above extends this JavaBean, exposing the searchProducts() method in its remote interface.)

So that's where I became confused.

(jpk)Your feedback is good.  So far you are easily the leading contender for the points.  However, I'm going to wait it out until this weekend to see if I get any other valuable info.  The problem with this type of question is that it there isn't a right or wrong answer...only a best answer...therefore I don't want to give the points away until I've given ample opportunity to find the best answer.

Expert Comment

ID: 7620922
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- points to jpk

Please leave any comments here within the next seven days.


Cleanup Volunteer

Expert Comment

ID: 7643006
Finalized as proposed


Community Support Moderator
Experts Exchange

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question