Ap100
Ap100

Reputation: 231

Elastic Search - Phrase Matching using Java

I am trying to do a Phrase matching to find similar Phrases.

Eg. Name field has following entries and all 3 should be evaluated to same :

  1. "USA Tech Company"
  2. "USA Tech Company Alabama"
  3. "USA Tech Company California"

Can you suggest a Java code that uses Phrase matcher or something similar to suggest that above entries in the Name field are same ( possibly higher score)

Thanks.

Upvotes: 0

Views: 1255

Answers (1)

Hosang Jeon
Hosang Jeon

Reputation: 1423

You may use match query using "phrase" type or "phrase_prefix" type for your case. In Java client, Elasticsearch provides two kinds of method you can use.

  • QueryBuilders.matchPhraseQuery(name, text);
  • QueryBuilders.matchPhrasePrefixQuery(name, text);

Try to test the scripts below to check the result.

curl -s -XPOST "http://localhost:9200/ap100/example/1" -d '{
    "name": "USA Tech Company"
}'
curl -s -XPOST "http://localhost:9200/ap100/example/2" -d '{
    "name": "USA Tech Company Alabama"
}'
curl -s -XPOST "http://localhost:9200/ap100/example/3" -d '{
    "name": "USA Tech Company California"
}'

curl -XGET "http://localhost:9200/ap100/example/_search?pretty" -d '{
    "query":{
        "match":{
            "name": {
                "type": "phrase",
                "query": "usa tech company",
                "slop": 2
            }
        }
    }
}'

curl -XGET "http://localhost:9200/ap100/example/_search?pretty" -d '{
    "query":{
        "match":{
            "name": {
                "type": "phrase_prefix",
                "query": "usa tech company"
            }
        }
    }
}'

Upvotes: 1

Related Questions