Rocky
Rocky

Reputation: 951

Issues with elasticsearch using play framework

I am a newbie to both Play and ElasticSearch and have been trying to configure them for a POC. I have used the CRUD module (Play 1.2.4) and have created a model called Book

@ElasticSearchable
@Entity
public class Book extends Model{

    @Required
    public String title ;
    @Required
    public String author ;
    @Required
    public String publisher ;
    public String binding ;
    @Required
    public double price ;
    public double discount ;
    @Required
    @MaxLength(4)
    public String releasedYear ;
    @Required
    public boolean inStock ;
    public String language ;
    public String deliveryTime ;
}

And have created a few records in the in-memory database H2 and have those records indexed in elasticsearch node ( I am using my local machine to run ES )

When I try to do a search using the ES admin interface (provided by default when we use the ES module in Play) , I have hit upon a strange issue

I have one book whose title is "Java for beginners" and I am trying to run a term query on the field title from the ES-Admin Interface.

{"query" : {"term" : { "title" : "Java for beginners" }}}

And it returns me

{
took: 3
timed_out: false
_shards: {
total: 5
successful: 5
failed: 0
}
hits: {
total: 0
max_score: null
hits: [ ]
}
}

which essentially means that there are no matching records

Strangely when I change my query to

{"query" : {"term" : { "title" : "beginners" }}}

it returns me the record as follows

{
took: 3
timed_out: false
_shards: {
total: 5
successful: 5
failed: 0
}
hits: {
total: 1
max_score: 0.19178301
hits: [
{
_index: models_book
_type: models_book
_id: 1
_score: 0.19178301
_source: {
title: Java for beginners
author: Bruce Eckel
publisher: Timburys
binding: Paperback
price: 450
discount: 10
releasedYear: 2010
inStock: true
language: English
deliveryTime: 3 days
id: 1
}
}
]
}
}

It will be of great help if someone can throw some light on this. Any help in the right direction will be greatly appreciated

Thanks

Upvotes: 0

Views: 552

Answers (1)

thnetos
thnetos

Reputation: 1326

When using a term query, the term being searched for is not analyzed, meaning normally it should be a single term. If you want to query for a string that needs to be analyzed, you should use the query_string query type.

This query should work for you:

curl -s "localhost:9200/test/_search" -d '
{
  "query":{
    "query_string":{
      "query":"Java for beginners"
    }
  }
}'

Upvotes: 2

Related Questions