Jump to content
johnnydoe

Full text site search function

Recommended Posts

Hi there!
I want to implement a "simple" full text search (no ajax needed) into a webpage that's built with processwire. I couldn't find any threads, tutorials or modules helping with this. Can anyone help me with this or point me to a place where I find information how to implement this? 

Thanks in advance!

 Jonas

Share this post


Link to post
Share on other sites

Hi,

you can find some basic search logic in the search.php of the default site profile: https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/templates/search.php

In https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/templates/_main.php#L61 you find the code for the search form.

This should get you started.

EDIT: If you should later decide to use AJAX, there is Soma's module: https://github.com/somatonic/AjaxSearch

  • Like 4

Share this post


Link to post
Share on other sites

Hi johnnydoe, welcome to the forum!

I think the easiest approach is to use the Processwire API.

Give a look at the Skyscrapper profile template files. Specifically, the search.php template file. Take a look at how the Processwire API is used to check for HTTP variables and sanitize the data received.

In the end of the search.php file you will find:

$content .= renderSkyscraperList(findSkyscrapers($selector)); 

..Which you can take a look here in the functions.php file, and it is the function that actually renders the markup from the PageArray retrieved by the selector queries. It is saved into $content, and rendered in _out.php.

You might also take a look at this page of the documentation explaining the $input variable.

https://processwire.com/api/variables/input/

  • Like 4

Share this post


Link to post
Share on other sites

Thank you very much for your help!

For now I took the search from the default site profile. 

But I still have 2 problems, for which I couldn't find a solution so far:

  1. As selector for the results I have now: "title~=$q"  
    ($q stands for the term I entered). This gives me all the pages that have exactly this word in their title. If I enter "kanzler" I won't get a page with "bundeskanzler" -> this seems to be more important for german - where words are often put together - than for english. but it's a german page.
    So far I couldn't find a selector that just checks if a title contains a string (I looked at http://cheatsheet.processwire.com). Is there a possibility to achieve this?
  2. I would like to search fields like body and some others too for the search term. But if I write "title|body~=$q" as e selector, it returns an error ("Error: Exception: Field does not exist: body"), because not all my templates have a body element, I guess? Is there a possiblity to search fields that are not in all templates?

Have a nice week!

 Jonas

Share this post


Link to post
Share on other sites

If you need partial matching you'd need to use the %= operator. It's using mysql's LIKE to get matches.

  • Like 3

Share this post


Link to post
Share on other sites

Thank you, I overlooked this one ^^

My other question (search several fields) is also finished... I turned out that I didn't have a field "body" at all. If it exists, there is no error.
 

  • Like 1

Share this post


Link to post
Share on other sites

I am trying to implement the search function from the demo into my website. I run into errors, though.

My search.php template starts like this:

Quote

<?php namespace ProcessWire;

// look for a GET variable named 'q' and sanitize it
$q = input()->get('q');

// sanitize to text, which removes markup, newlines, too long, etc.
$q = sanitizer()->text($q);

Already at line 4 it throws an error:

Quote

Fatal Error: Uncaught Error: Call to undefined function ProcessWire\input()...

I am lost... What am I doing wrong? It somehow must have to do with Processwire syntax, but the code is taken directly from the demo... so why wouldn't it work?

Share this post


Link to post
Share on other sites

Ah... thank you. That at least takes care of the errors... not quite there, but closer...

Still confused about the PW syntax 🙄

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By michelangelo
      Hello guys, I am building a sort of an archive. Relatively simple, although I have about 8000 records, each with 15 fields (text, int, images, url). I created a crude search system with a form (emulating the famous Skyscrapper example) to filter through the system. Everything works but it is quite slow... I have 2 questions which are related:

      1. How can I search through the database?
      2. What is a good practice to display many records like these?
      -----------------------------------------
      1. I am retrieving the results with
      $songs = $pages->findMany('template=nk-song'); Then I do a foreach to render them all. I am unsure if that is a good way. If I render all of them on the page, it creates thousands of divs with a bit of text, and this can take a while (10s-15s).
       
      2. This one is even worse :D as every time I retrieve my desired records with something like this:
      $page->find("field_to_search_through~=my_query_string") I get between 20 and 200, but when I render them I am creating iframes with YouTube videos and that can take up to 10s to finish. I "solved" it by only loading the iframes if they are in view with IntersectionObserver on the client-side. But I feel there is a more precise PHP / ProcessWire approach.
       
      Just to clarify, I started doing all of this custom rendering and querying because tools like ElasticSearch or SearchEngine were a bit complicated and I needed a simple to retrieve information and then display it in my own way.
      Thank you!
    • By snobjorn
      I have a website with multiple content types that I want to be accessible through search. I really like the live search on processwire.com, that sorts content types while typing. I tried to find the code to recreate this, with no luck. Does anyone know if this is jquery, specific jquery plugins, json/xml cached files, and what kind of PHP code is used? Any tip that point me in the right direction would be much apperciated.
      The search result listing seems fairly easy to create with sorting through parameters.
    • By Anders
      I want to allow full text search on my site. There is a very nice solution that comes right out of the box:
      $selector = "title|body~=$q, limit=50"; This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?
      I can see three possible paths, but I am not very fond of any of them:
      Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body? Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!
    • By jds43
      Hello,
      I have a search page loosely based on Skyscrapers where I'm parsing a selector with options 'beds', 'bathrooms', 'size' fields. It is working well until I select 'Any' after I've run a search. This is where no results are returned (/?beds=&bathrooms=&size=&submit=). I want it to reset and show all results.
      I hope this isn't too vague.
       
×
×
  • Create New...