2

I'm writing a Seam-based application, making use of JPA/Hibernate and Hibernate Search (Lucene). I have an object called Item that has a many-to-many relation to an object Keyword. It looks like this (some annotations omitted):

@Indexed
public class Item {

   ...

  @IndexedEmbedded
  private List<Keyword> keywords;

   ...
}

@Indexed
public class Keyword {

   ...

  @Field
  private String value;

   ...
}

I'd like to be able to run a query for all Item object that contain a particular keyword value. I've setup numerous test objects in my database, and it appears the indexes are being created properly. However, when I create and run a query for "keywords.value" = <MY KEYWORD VALUE> I always get 0 results returned.

Does Hibernate Search/Lucene have the ability to run queries of this type? Is there something else I should be doing? Are there additional annotations that I could be missing?

Shadowman
  • 11,150
  • 19
  • 100
  • 198

1 Answers1

1

Hibernate Search is perfectly suited for that kind of queries; but it can be done in a simpler way.

On your problem: text indexed by Hibernate Search (Lucene) is going to be Analysed, and the default analyser applies:

  1. Lower casing of the input
  2. Splitting in separate terms on whitespace

So if you're defining the queries as TermQuery (I'm assuming that's what you did, as it's the simplest form), then you have to match against the lower case form, of a token (with no spacing).

Bearing this in mind, you could dump all your keywords in a single Blob String on the Item entity, without needing to map it as separate keywords, chaining them in a single string separated by whitespaces.

Sanne
  • 6,027
  • 19
  • 34