Skip to content

Conversation

@tannerwelsh
Copy link

Addresses issue #100.

If configuration is set to

:using => {
  :tsearch => { :highlight => true }
}

then queries will return records with an additional pg_highlight attribute.

Currently uses the defaults set by the Postgres function ts_headline, as specified here: http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-HEADLINE

Also supports custom delimiters

:using => {
  :tsearch => { 
    :highlight => { 
      :start_sel => "begin", 
      :stop_sel => "end" 
    }
  }
}

will override the default <b> and </b> query delimiters in the highlighted results.

NOTE: the pg function used (ts_headline()) is not supported on pg versions < 9.0. I know that the gem is intended to support 8.0 and above, but this seemed like a useful enough feature get started. Any way to work around?

If configuration is set to

```
:using => {
  :tsearch => {:highlight => true}
}
```

then queryies will return records with an additional `pg_highlight`
attribute.

Currently uses the defaults set by the Postgres function ts_headline, as
specified here:
http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-HEADLINE
Improves the "SELECT" clause of the statement by factoring out added
fields into their own methods to be added as needed.
Should search within all provided documents
Will generate SQL to run the ts_headline() function with the provided query.
Using the ts_search options

```ruby
{ highlight: { start_sel: "begin", stop_sel: "end" } }
```

will override the default "<b>" and "</b" query delimiters in the highlighted results.
@tannerwelsh
Copy link
Author

So the build is failing because Rubocop wants class definitions to have <= 100 lines of code, and /lib/pg_search/features/tsearch.rb is 107. :/

I could resolve by either:

  1. Extracting into a module
  2. Changing the Rubocop config

Do you have a preference @nertzy?

@nertzy
Copy link
Collaborator

nertzy commented Jan 8, 2015

It would be better to refactor some of the methods into service objects. For instance, tsquery_for_term is way too complicated.

But for now it would be OK with me to add a Rubocop comment that turns off the file size check for just tsearch.rb until we can get a good refactor in. Just add a line to the top of the file that turns off that rule:

# rubocop:disable Metrics/ClassLength

@marshall-lee
Copy link

This is awesome! 👍

@tannerwelsh
Copy link
Author

Thanks, @nertzy. I'd rather do it right the first time, so I'll refactor them out as suggested.

@tannerwelsh
Copy link
Author

Ok, so here's the deal. I cleaned up this PR, added documentation, and added a workaround to appease Rubocop.

Then, in a separate branch, I did some serious refactoring.

I didn't want to bundle all of it into this one PR, so we can do it in two phases:

  1. If this PR looks good, then you can merge and the new feature will be available.
  2. I'll make another PR with the refactorings so that the code is actually a little cleaner (not perfect, but better).

What do you think, @nertzy? Will that work for you?

@nertzy
Copy link
Collaborator

nertzy commented May 15, 2015

I'm sorry that I've taken so long to address this pull request. Unfortunately, the new subquery structure in pg_search 1.0 breaks this pull request. I spent a few minutes trying to refactor what you've built to act similarly to how .with_pg_search_rank works, but to no avail.

So I'm going to close this for now. I'd gladly accept a pull request based off of this that adapts things to the new subquery, and that makes the highlight optional through something like a .with_pg_search_highlight method chained onto the scope.

@nertzy nertzy closed this May 15, 2015
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 5, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 5, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 5, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 5, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 5, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 19, 2016
@tannerwelsh
Copy link
Author

Hey @nertzy no worries, I got pulled in other directions too. Sadly this is not a high priority for me anymore, so don't think I'll be able to work on the feature anytime soon.

Thanks for the helpful suggestions and feedback earlier though! Hope that #280 is able to make it through by building on this one. 😄

ceritium added a commit to ceritium/pg_search that referenced this pull request Jan 20, 2016
ceritium added a commit to ceritium/pg_search that referenced this pull request Feb 2, 2016
@olivierlacan
Copy link

@nertzy Was documentation for this feature merged into the master branch README by mistake? I keep trying to use with_pg_search_highlight on a search scope and the method always comes up as undefined.

@olivierlacan
Copy link

By the way I'm referring to https://github.com/Casecommons/pg_search/blob/ff1af346322ca54808b3f53081149b8cac55bb76/README.md#highlight-postgresql-90-and-newer-only

The recommended configuration causes the following error:

ArgumentError: Unknown key: :start_sel.

@nertzy
Copy link
Collaborator

nertzy commented Apr 8, 2016

It's not an error. It's merged to master but not yet in a released gem version, I believe. I'll try to get a new version cut very soon, sorry for the confusion!

@olivierlacan
Copy link

olivierlacan commented Apr 8, 2016 via email

@sintro
Copy link

sintro commented May 3, 2016

There are still error in documentation (the sample content is:

:using => {
  :tsearch => { 
      :start_sel => "begin", 
      :stop_sel => "end" 
  }
}

but not

:using => {
  :tsearch => { 
    :highlight => { 
      :start_sel => "begin", 
      :stop_sel => "end" 
    }
  }
}

)
Also I found that there is error when trying to use with_pg_search_highlight method in combination with associated_against, the error description is something like

table "pg_search_3be97ef13e109a88624534" is absent in FROM-clause 

@heisters
Copy link
Contributor

@sintro I fixed the documentation bug in #312

PlayPavilion added a commit to PlayPavilion/kbengine that referenced this pull request Aug 15, 2025
HollyBlande added a commit to HollyBlande/analysis that referenced this pull request Sep 8, 2025
Hugoonlin added a commit to Hugoonlin/Vevorde that referenced this pull request Sep 19, 2025
lechiumemedai added a commit to lechiumemedai/lechiumemedai that referenced this pull request Sep 26, 2025
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

Successfully merging this pull request may close these issues.

7 participants