Вопрос

i need your help. I want to get the matched string in a full text sunspot search. Thats how my code looks like at the moment

search2 = WebsiteEntry.search do
    fulltext params[:q]
  end

  search2.each_hit_with_result do |hit, res|
    @match = <Here I need your Help, i need the substring on which sunspot made a hit>
    @results.push SearchResult.new(res, hit.score, @match)
  end
end

Now, if i´m searching for the word "test" sunspot looks for everything where "test" is a substring and case insensitive. For example: Sunspot return the matched words "FirstTest" or "TEST2" or "testit".

Is it possible to get the matched string?. I need the string on which sunspot hitted

From "FirstTest" in need the hit "Test". Because i would like to display where sunspot founded a match. So, from "FirstTest" i need the substring "Test". From "TEST2" i need "TEST" and so on.

Thank you.

Это было полезно?

Решение

i have now the solution for my problem. The "hit" - Objekt in the "each_hit_with_result" function return a list with the matched substrings, called "highlight"

search2 = WebsiteEntry.search do
    fulltext params[:q] do
      highlight :domain
    end
  end

  search2.each_hit_with_result do |hit, res|
    hit.highlight(:domain).format { |word| puts "highlight: "+word}

No I´m getting each matched string form the full text search. The Problem was, that i didn`t mark my attribute as highlight. Thx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top