Display Name
Display Name

Reputation: 139

Lucene substring matching

What is the best way to check if String a is part of String b in Lucene. For example: a = "capital" and b = "Berlin is a capital of Germany". In this case b contains a and fits the requirement.

Upvotes: 1

Views: 912

Answers (1)

Allen Chou
Allen Chou

Reputation: 1237

I think your problem can be treated as some field contains certain term or not. The basic TermQuery should be enough to solve your problem, in most analyzers, "Berlin is a capital of Germany" will be analyzed as "berlin", "capital" "germany"(if you use the basic stop words)

// code in Scala
new TermQuery(new Term("contents", "capital"))

you can also use PhraseQuery to solve your problem(though, your problem is not the most suitable scenario for PhraseQuery).

val query = new PhraseQuery();
query.add(new Term("contents", "capital"))

Lucene In Action 2nd, 3.4 Lucene’s diverse queries introduces all kinds of Query used in Lucene. I suggest you have a read and that might help.

Upvotes: 1

Related Questions