Keyword Search in J2EE

Posted on 2001-06-23
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 300 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image decoding from Camera 3 72
printing a file in reverse order is easy in recursion rather than in iteration 3 48
Error trying to install RTMT Win7 5 38
table example 4 19
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now