Mohamed Nabli
Mohamed Nabli

Reputation: 1649

Hibernate Search : fetch indexed field containing the given key word

I'm new at using Hibernate Search, and here is my implementation: I start with the entity and its field

@AnalyzerDef(name = "ngram",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class ),
filters = {

  @TokenFilterDef(factory = StandardFilterFactory.class),
  @TokenFilterDef(factory = LowerCaseFilterFactory.class),
  @TokenFilterDef(factory = StopFilterFactory.class),
  @TokenFilterDef(factory = NGramFilterFactory.class,
    params = {
      @Parameter(name = "minGramSize", value = "3"),
      @Parameter(name = "maxGramSize", value = "3") } )
}
)

@Indexed
@Entity
@Table(name="USERS")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Field
    @Column(nullable=false,length=30,name="first_name")
    private String firstName;

    @Column(nullable=false,length=30,name="last_name")
    private String lastName;


    @Column(length=128)
    @Field(analyzer=@Analyzer(definition="ngram"))
    private String login;

    @Column(length=255)
    private String password;

In my database i have 2 records with the following data :


|id | first_name | last_name | login | password |


|1 | NULL | NULL | mednabli | * | |2 | NULL | NULL | med-nabli | * |

when I apply a research on the field login I display the login of results I get:

typed keyword : med

result : mednabli med-nabli

typed keyword : med-

result : (nothing) while I need to get med-nabli

typed keyword : nabli

result : med-nabli while I need to get mednabli med-nabli

typed keyword : kamed

result : (nothing) while I need to get mednabli med-nabli

So please is there a way to make the search get to desired result, is there an analyzer I should rely on it to make my application works As I want to ??

Upvotes: 0

Views: 97

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10539

So, first thing is that you can't get what you want with only one field. So you need to create another field (see @Fields annotation to define several fields on a single property). This new field should have a keyword tokenizer and a lowercase filter. You should then build your query by querying both fields (with a should) with the query on the second one being a wildcard query "yoursearch".

This will solve all your cases, except this one: "typed keyword : kamed". As for this one, can you provide how you build and execute your query?

NB: It's not a good idea to define a Stop filter on your login search so you should remove it.

Upvotes: 1

Related Questions