Skip to content

Search on attributes does not work #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stabenfeldt opened this issue Jan 22, 2014 · 9 comments
Closed

Search on attributes does not work #1

stabenfeldt opened this issue Jan 22, 2014 · 9 comments

Comments

@stabenfeldt
Copy link

Hi again,

I updated the Rails / MongoDB proof of concept app to use elasticsearch-rails instad of ReTire. Unfortunately I still have trouble searching. Am I doing it wrong?

models/ad.rb

class Ad
  include Mongoid::Document
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  field :title, type: String
  field :body, type: String
end
 rails console
Loading development environment (Rails 4.0.0)
[2] pry(main)> Ad.import
NoMethodError: undefined method 'as_indexed_json' for #<Ad _id: 52d8e35d4d61724105010000, title: "martin", body: nil>
from /Users/martins/.rvm/gems/ruby-2.0.0-p353@ReTire/bundler/gems/elasticsearch-rails-04a637725dbf/elasticsearch-model/lib/elasticsearch/model/adapters/mongoid.rb:80:in `block in __find_in_batches'
[3] pry(main)> Ad.search('*').size
=> 10
[4] pry(main)> Ad.search('title:"martin"').size
=> 0
[5] pry(main)> Ad.where(title: 'martin').size
=> 1
@stabenfeldt
Copy link
Author

Adding require 'elasticsearch/model' to the Ad model makes no difference.

@karmi
Copy link
Contributor

karmi commented Jan 22, 2014

You don't have to require anything -- Bundler takes care of that.

There's a full Mongoid example here: https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/examples/mongoid_article.rb, I advice you to try it out first.

Also, be sure to generate a plain Rails app [https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-rails#usage], to get a feeling for how it works.

@stabenfeldt
Copy link
Author

Thanks for the links. I copied some from mongoid_article.rb, but I still can't search on the title attribute.
Would you be so kind and fork my TireMongoDB app and see if you manage to search by title?

> Ad.search('martin').records.first
=> #<Ad _id: 52dfccf44d61720a7c000000, title: "martin", body: nil>
> Ad.search(title:'martin').records.first
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[_3vqU7ExR8qSv2yQ8kwhqQ][ads][4]: SearchParseException[[ads][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"multi_match\":{\"query\":{\"title\":\"martin\"},\"fields\":[\"title^10\",\"content\"]}},\"highlight\":{\"pre_tags\":[\"\\u003Cem class=\\\"label label-highlight\\\"\\u003E\"],\"post_tags\":[\"\\u003C/em\\u003E\"],\"fields\":{\"title\":{\"number_of_fragments\":0},\"content\":{\"fragment_size\":25}}}}]]]; nested: QueryParsingException[[ads] [match] query does not support [title]]; }{[_3vqU7ExR8qSv2yQ8kwhqQ][ads][0]: SearchParseException[[ads][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"multi_match\":{\"query\":{\"title\":\"martin\"},\"fields\":[\"title^10\",\"content\"]}},\"highlight\":{\"pre_tags\":[\"\\u003Cem class=\\\"label label-highlight\\\"\\u003E\"],\"post_tags\":[\"\\u003C/em\\u003E\"],\"fields\":{\"title\":{\"number_of_fragments\":0},\"content\":{\"fragment_size\":25}}}}]]]; nested: QueryParsingException[[ads] [match] query does not support [title]]; }{[_3vqU7ExR8qSv2yQ8kwhqQ][ads][1]: SearchParseException[[ads][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"multi_match\":{\"query\":{\"title\":\"martin\"},\"fields\":[\"title^10\",\"content\"]}},\"highlight\":{\"pre_tags\":[\"\\u003Cem class=\\\"label label-highlight\\\"\\u003E\"],\"post_tags\":[\"\\u003C/em\\u003E\"],\"fields\":{\"title\":{\"number_of_fragments\":0},\"content\":{\"fragment_size\":25}}}}]]]; nested: QueryParsingException[[ads] [match] query does not support [title]]; }{[_3vqU7ExR8qSv2yQ8kwhqQ][ads][2]: SearchParseException[[ads][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"multi_match\":{\"query\":{\"title\":\"martin\"},\"fields\":[\"title^10\",\"content\"]}},\"highlight\":{\"pre_tags\":[\"\\u003Cem class=\\\"label label-highlight\\\"\\u003E\"],\"post_tags\":[\"\\u003C/em\\u003E\"],\"fields\":{\"title\":{\"number_of_fragments\":0},\"content\":{\"fragment_size\":25}}}}]]]; nested: QueryParsingException[[ads] [match] query does not support [title]]; }{[_3vqU7ExR8qSv2yQ8kwhqQ][ads][3]: SearchParseException[[ads][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"multi_match\":{\"query\":{\"title\":\"martin\"},\"fields\":[\"title^10\",\"content\"]}},\"highlight\":{\"pre_tags\":[\"\\u003Cem class=\\\"label label-highlight\\\"\\u003E\"],\"post_tags\":[\"\\u003C/em\\u003E\"],\"fields\":{\"title\":{\"number_of_fragments\":0},\"content\":{\"fragment_size\":25}}}}]]]; nested: QueryParsingException[[ads] [match] query does not support [title]]; }]","status":400}
from /Users/martins/.rvm/gems/ruby-2.0.0-p353@ReTire/gems/elasticsearch-transport-0.4.7/lib/elasticsearch/transport/transport/base.rb:132:in `__raise_transport_error'

@stabenfeldt
Copy link
Author

➜  curl -XGET http://localhost:9200
{
  "ok" : true,
  "status" : 200,
  "name" : "Vashti",
  "version" : {
    "number" : "0.90.9",
    "build_hash" : "a968646da4b6a2d9d8bca9e51e92597fe64e8d1a",
    "build_timestamp" : "2013-12-23T10:35:28Z",
    "build_snapshot" : false,
    "lucene_version" : "4.6"
  },
  "tagline" : "You Know, for Search"
}

@karmi
Copy link
Contributor

karmi commented Jan 22, 2014

Would you be so kind and prepared a plain & simple application as you did in karmi/retire#915? .)

@stabenfeldt
Copy link
Author

Sure, that's what I've done. :)
You can find a plain, stripped down Rails app here: https://github.com/stabenfeldt/TireMongoDB/

On Wed, Jan 22, 2014 at 3:33 PM, Karel Minarik [email protected]:

Would you be so kind and prepared a plain & simple application as you did
in karmi/retire#915 karmi/retire#915? .)


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-33027811
.

Martin Stabenfeldt
Tlf: +47 93441707

@karmi
Copy link
Contributor

karmi commented Jan 22, 2014

Seems to work for me with all the stock config:

# bundle exec rails console:

Ad.__elasticsearch__.client.transport.logger = Logger.new(STDERR)

Ad.create title: 'FooBar'

I, [2014-01-22T15:46:08.047253 #87917]  INFO -- : PUT http://localhost:9200/ads/ad/52dfd9b0616c75576d010000 [status:201, request:0.026s, query:n/a]
D, [2014-01-22T15:46:08.047456 #87917] DEBUG -- : > {"body":null,"title":"FooBar"}
D, [2014-01-22T15:46:08.047502 #87917] DEBUG -- : < {"ok":true,"_index":"ads","_type":"ad","_id":"52dfd9b0616c75576d010000","_version":1}
I, [2014-01-22T15:46:08.047746 #87917]  INFO -- : curl -X PUT 'http://localhost:9200/ads/ad/52dfd9b0616c75576d010000?pretty' -d '{"body":null,"title":"FooBar"}'

D, [2014-01-22T15:46:08.047860 #87917] DEBUG -- : # 2014-01-22T15:46:08+01:00 [201] (0.026s)
#
D, [2014-01-22T15:46:08.048225 #87917] DEBUG -- : # {"ok":true,"_index":"ads","_type":"ad","_id":"52dfd9b0616c75576d010000","_version":1
# }

Ad.__elasticsearch__.refresh_index!

Ad.search('title:foobar').size

I, [2014-01-22T15:46:26.248997 #87917]  INFO -- : GET http://localhost:9200/ads/ad/_search?q=title%3Afoobar [status:200, request:0.022s, query:0.017s]
D, [2014-01-22T15:46:26.249082 #87917] DEBUG -- : < {"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.5945348,"hits":[{"_index":"ads","_type":"ad","_id":"52de42c1616c751139000000","_score":0.5945348, "_source" : {"body":null,"title":"foobar"}},{"_index":"ads","_type":"ad","_id":"52dfd934616c75576d000000","_score":0.5945348, "_source" : {"body":null,"title":"FooBar"}},{"_index":"ads","_type":"ad","_id":"52dfd9b0616c75576d010000","_score":0.30685282, "_source" : {"body":null,"title":"FooBar"}}]}}
I, [2014-01-22T15:46:26.249201 #87917]  INFO -- : curl -X GET 'http://localhost:9200/ads/ad/_search?pretty&q=title%3Afoobar'

D, [2014-01-22T15:46:26.249270 #87917] DEBUG -- : # 2014-01-22T15:46:26+01:00 [200] (0.022s)
#
D, [2014-01-22T15:46:26.250465 #87917] DEBUG -- : # {"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.5945348,"hits":[{"_index":"ads","_type":"ad","_id":"52de42c1616c751139000000","_score":0.5945348,"_source":{"body":null,"title":"foobar"}},{"_index":"ads","_type":"ad","_id":"52dfd934616c75576d000000","_score":0.5945348,"_source":{"body":null,"title":"FooBar"}},{"_index":"ads","_type":"ad","_id":"52dfd9b0616c75576d010000","_score":0.30685282,"_source":{"body":null,"title":"FooBar"}}]}
# }

=> 3

@stabenfeldt
Copy link
Author

Ouch, this is embarrassing! 😊
My previous search failed because I did Ad.search(title:'martin')
The correct syntax is Ad.search('title:martin').size

Thanks for helping out again!! 👍 👍 👍

@karmi
Copy link
Contributor

karmi commented Jan 22, 2014

@stabenfeldt Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants