I created a search Index with multiple fields, and I am using the search API programmatically, is there a way to limit the search only on selected fields and not in all of theme?

Comments

ali-3a created an issue. See original summary.

drunken monkey’s picture

Component: General code » Framework
Category: Feature request » Support request
Status: Active » Fixed

Sure: see \Drupal\search_api\Query\QueryInterface::setFulltextFields().

ali-3a’s picture

Status: Fixed » Active

Thank you it worked,
I tried it befor asking just like it is in the documentation:
$query->setFulltextFields(['title', 'name', 'body']);
I didn't indexed the title or the body (I don't know what is name), and when I didn't get any error I thought that the line was ignored or it has another purpose, I think it should throw an error not just a warning message in the log as I found later:
Search keys are given but no fulltext fields are defined.

drunken monkey’s picture

Title: Select fields for search » Throw exception when query has keywords but no fulltext fields
Version: 8.x-1.11 » 8.x-1.x-dev
Component: Framework » Database backend
Category: Support request » Task
Status: Active » Needs review
StatusFileSize
new2.47 KB

Hm, you’re right, a warning is a bit little in that case.
Patch attached, please test/review!

ali-3a’s picture

Looks great now, Thanks.

laravz’s picture

Status: Needs review » Reviewed & tested by the community

The patch applied successfully to the latest dev. The only change in code is that now a SearchApiException is thrown when the fulltext fields are not defined instead of logging a warning. Seems like an improvement to me.

  • drunken monkey committed f60fd5b on 8.x-1.x
    Issue #3039142 by drunken monkey, ali-3a, LaravZ: Fixed all results...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Great to hear, thanks for reviewing!
Committed.
Thanks again!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.