I have 5 nodes title with some keywords:

  1. this is my node one with some keywords
  2. this is my second title with some keywords
  3. this is my another title with second keyword
  4. this is my another title second keyword
  5. this was my last node title

My search query is: "second title"

when i search with only title keyword, query return 2,3,4, and 5 well, but when i search second title, query return only 2. So, how can i extend my query to return nodes as 2,4,3 and 5 in the last position (since 5 has only one keyword match).

This is my query:

$query = db_select('node', 'n')
         ->condition('n.title', '%' . db_like($_GET['Keywords']) . '%', 'LIKE')

Updated: search keywords can more then 2 words, but query should return as much words matches.

please help!


  • Sanitize that input! Id advise using Search API + View with a fulltext search. Solr would be best fit for broadest yet relevant match. – Kevin Mar 21 '18 at 21:03
  • @Kevin, thanks for your quick reply, but here i don't use views and search api module. I created my search results page using custom module. And honestly i am not much expert to integrate search api module with my custom module. Have you any advice to integrate? – Tazim Mar 21 '18 at 21:16

You want db_or().

$or = db_or()->condition('n.title', '%' . db_like('second') . '%', 'LIKE')->condition('n.title', '%' . db_like('title') . '%', 'LIKE');
$results = db_select('node', 'n')
|improve this answer|||||
  • Yes, you updated answer working well, but have you any idea, how do i set orderBy parameters by maximum match? – Tazim Mar 21 '18 at 21:37
  • maybe this answer will help me stackoverflow.com/a/8534668/3770000, but i can't figure out, how to use it with your code... could you please visit this? – Tazim Mar 21 '18 at 21:44
  • That’s a different question. – Jaypan Mar 21 '18 at 21:46
  • what should i do? open a new question? I have already asked this question. – Tazim Mar 21 '18 at 21:50
  • Go back to that question then please. Also since this answered your question please mark it as the accepted answer. – Jaypan Mar 21 '18 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.