3

I would like execute this exemple :

$ curl -XGET 'http://localhost:9200/twitter/tweet/1/_mlt?mlt_fields=tag,content&min_doc_freq=1'

with Tire gem. It's poossible ?

My goal to search document related to another document.

Bastien D
  • 1,395
  • 2
  • 14
  • 26

2 Answers2

3

It is not implemented directly in tire. Karmi, however, has implemented it as a tire extension in the tire-contrib repository.

  • Source Code: more_like_this.rb
  • Add by adding gem 'tire-contrib'
  • more_like_this_field(:tag, like_text, options = {min_doc_freq: 1})
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • Thanks for your aswer. I am not sure to build similar request with this extension. Your exemple use "like_text" and not document id to find related document. – Bastien D Aug 25 '13 at 21:17
  • @BastienD If I have understood this correctly then is this method part of the Tire DSL which means you can add it to a search block in tire like any other method (query, size, etc.). The document id is defined in your query and you leave the like text null. – Konrad Reiche Aug 25 '13 at 21:32
0

Okay the internet forgot to include a single example of this call (including the source project), so here is one style of it.

related_articles = Article.search {
  query {
    more_like_this("#{current_article.title} #{current_article.body}",
      fields: [:title, :description],
      percent_terms_to_match: 0.1,
      min_term_freq: 1,
      min_doc_freq: 1
    )
  }
}
puts related_articles.results.count
puts related_articles.results.first.title if related_articles.present?

The gotcha here are the min_term_freq and min_doc_freq params above. They default to 2 and 5 respectively in ElasticSearch, which makes it easy to get confused while testing this.

mahemoff
  • 44,526
  • 36
  • 160
  • 222