I am trying to override the modulename_search_api_query_alter with a custom module to add an additional where clause to the query which is composed by SearchAPI.
I have only indexed the terms for nodes. I want to find exactly nodes only that has the terms or at least one of the term that I am searching for. For example if I search nodes that has term1 and term2 I want to get nodes that has only term1 and term2 or nodes that has term1 or term2. SearchAPI currently returns nodes that has term1 term2 also term3 term4 and this is what I do not want.
SearchAPI composes the following SQL command:
SELECT t.item_id AS item_id, SUM(t.score) AS score
FROM (
SELECT t.*
FROM search_api_db_myinde_field_tags_name t
WHERE (
(word = 'term1')
OR
(word = 'term2')
)
) t
GROUP BY t.item_id
HAVING (COUNT(DISTINCT t.word) >= :subs2)
ORDER BY score DESC LIMIT 10 OFFSET 0
And the following is what I want.
SELECT t.item_id AS item_id, SUM(t.score) AS score
FROM (
SELECT t.* FROM search_api_db_myinde_field_tags_name t
WHERE (
(word = 'term1') OR (word = 'term2')
)
) t
WHERE (
select count(*) as cnt from taxonomy_index as ti where ti.nid = t.item_id having count(*) <= 2
)
GROUP BY t.item_id HAVING (COUNT(DISTINCT t.word) >= 2)
ORDER BY score DESC LIMIT 10 OFFSET 0
This query's where clause says, find nodes where the count of terms for a node is maximum the terms that searched for. Any idea?
Comments
Comment #1
drunken monkeyThis is not possible with the Search API query itself.
Just use
hook_query_search_api_db_search_alter()instead to manipulate the database query directly.