Developer Tips: How Prestashop Search Works

May 17, 20122013

Search is an integral component of any eCommerce project. The more efficient it is, the better seller’s business is doing. Search significance is proven by a huge success of Google – a company that provides relevant answers for user requests. Today we will examine technical features of Prestashop search and share details, available for average users.

14

Back Office -> Preferences -> Search part is configured by users and provides opportunities for search to be adjusted flexibly.

On the picture above, the system performance will be high in any case since the number of products in the directory is small. But there are projects, in which the directory is very large and among the variety of products customers need to find the one that matches their request precisely.

Indexed products parameter indicates the amount of products from the catalog that are available. There are two links: Add missing products to index and Re-build entire index. The former activates the process of indexation, upon completion of which index includes products that were absent for some reason. The latter clears search_index and search_word tables and sets the «indexed» flag for all items in the product table to zero. Then a complete re-indexation occurs. It’s desirable to configure a cron job for re-indexation at the time when site traffic is low and the server load is minimal. The process of re-indexation is very resource-intensive as far as each non-indexed product is processed one by one, language by language, although the products are processed 50 by 50 in order to avoid overloading MySQL.

Ajax search allows displaying search results “on the fly”, i.e. when the user enters a search query, which exceeds the Minimum word length value, the server receives the request and search results are shown in a dropdown menu. Blacklisted words help adding words to be extracted from (Seacrh :: sanitize ($ string, $ id_lang, $ indexation = false)), for instance, prepositions or obscenities. This will reduce the weight of the table with indexed words.

Instant search derives results not to the dropdown list, but displays them “on the fly” in the directory.

Weight section allows setting priorities for search requests, i.e. items with search words in the Reference field will appear higher than products, containing the same word in the Description field.

This is the required minimum for users to know. Now we will take a closer look at the technical side of search and indexation. The static function Search :: indexation ($ full = false, $ id_product = false) is responsible for the process of indexation and $weightArray is in charge of index fields and search priorities. Indexation is running through necessary fields in different tables and picks out the data. Then each sentence is divided into words, words from the Blacklist and those less than Minimum word length are removed and data is saved to search_word table. The image below introduces the result of indexation of seven products, reflected in 1551 in the search_word table!

This is the structure of the main tables, involved in the search:

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor

The search_word table stores indexed keywords for each language. They are connected through the id_word field with the search_index table, where their priorities and products they are related to are defined.

If you need to insert another field to the index, it should be brought to the $weightArray and added to the SQL request. The code below adds location field  to the index field.

 

The Search :: find ($ id_lang, $ expr, $ pageNumber = 1, $ pageSize = 1, $ orderBy = ‘position’, $ orderWay = ‘desc’, $ ajax = false, $ useCookie = true) function performs the search. It analyzes the search request, removing characters less than Minimum word length, and finally performs the search. It should be noted that Ajax and standard search are done by different SQL requests.

Expect to read about changing Prestashop standard search to sphinx soon.


Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

46 Comments

  1. This is good, but the search function is entirely about products. What about when a customers wants to search something from CMS pages?

  2. Hi, i have 260 000 products, and the search index can’t search everything (just 20 000) do you have any idea to force this ?
    thanks

  3. Nice article. What about if we would like to get results just from 1 category? Could Prestashop do that?

  4. Javier,

    Prestashop cannot do that as there is no such thing as default advanced search, which would specify such patameters.

  5. Hi, I would like to search for the ID also, is that possible? And how?? I am new to PrestaShop…

    // Sara

  6. Sara,

    There is no standard possibility, but you can override Search :: indexation method so that the id_product field could get in the index as well.

  7. Hi Alex, thanks for your nice article.
    You know how I can set up the prestashop search engine to search only in the title of the product?
    Thanks
    Giovanni

  8. Giovanni

    You need to override 2 functions of the class Search: getProductsToIndex and indexation.

    In the function getPrоduсtsTоIndex you need to define the fields which will participate in creating an array for indexing:

    In the method, which is directly responsible for indexing, you need to remove tags and features from the index. As for the attributes – I would recommend to leave them.

    After that, you need to reindex.

  9. Alex, we’re wanting to be able to search by supplier name also. To include supplier name as you did with the location field (from your previous post) would you go about that same premise to be able to search on supplier name (overriding Search: getProductsToIndex and indexation) ? Thanks.

  10. Alex,

    Thanks for this very usefull post.
    I’d like to ask you an advice to solve the following problem:
    My site is french and russian. Every product as title and attributes in french (or latin transcript) and russian (cyrillic).
    When I search a word written in cyrillic in the french interface there is no result. When I search the same word in the russian interface I have results. (and same problem in the other way when searching latin words in russian interface)
    How could it be possible to “liberate” the searched fields from the current FO language ?

    Thanks in advance !

  11. Hello and thank you for the article!

    I have a question about the search function in Prestashop.

    If i have a product code of GBH26VLI how can I have prestashop find it also by variations of the code, liek GBH 26, GBH 26VLI etc without adding tags??

  12. Ron,

    Sorry for the late response. In a few days I will post an article and will attach a module to include ‘supplier name’ into search.

  13. François,

    This happens because SQL query takes into account the id_lang parameter, which contains the language id of your current locale. To make search in the way you want it, you need to remove this parameter by overriding Search::find()

    You need to remove id_lang in the following parts:
    1.

    2.

  14. Terry,

    As a workaround, you can set customize logic: if your search is used only to search for code it is possible, for example, to remove all spaces. Or to create one additional field where users shall enter their code and to remove spaces there.

  15. Very nice article!!! Thank you.

    I have a question, though.

    Assume that I have a product with reference “STAR – 123”

    If I search “STAR – 123”, a result is found but if I search “STAR-123” removing the spaces, no result. Similary, if I search “”STAR123” removing the dash and spaces, no result again. Then if we try “S- TAR – 123” adding a new dash and space, again, no result.

    But many customers make their searches by entering what they remember. They don’t pay attention to punctuation.

    How can I overcome this problem?

    Thanks.

  16. Why i have indexed products 1138/1139… where is the other product? is a frequently bug or what happens?

  17. jorgevrgs,

    There are still no products after reindexation? The problem may be that products are saved wrongly.

  18. Hey, thanks for the article – very informative! If I understand this correctly, you cannot limit search to single specific field (that the user chooses) with the default setup. So, if one were to allow the visitor to search in only name or only short description – can’t I just search the product table directly? Or does the pair of index table improve search performance in addition to implementing the weight system?

  19. RickM,

    Yes, search sadly operates with the whole index table no matter what you search for – for instance, in names only. In order to search in only name the search module should be modified substantially. Indexation as well as search engine logic itself should be changed.

  20. hello alex, hope you are great, my question is how do i hide or eliminate the value “search”? i don´t want to show “search” in the button submit.

    thanks for your help

    Citlalli

  21. Citlalli,

    not sure if I understood you properly, but if you do not want to have the Submit button, simply delete it from the file

    themes/your_theme/modules/blocksearch/blocksearch.tpl

  22. Hi Alex,

    Thanks for this nice post

    Is writing a post about using sphinx search whith prestashop is still planned ?

    Thanks in advance.

    Regards

  23. Thank you, Antoine. This is quite an interesting topic, however, unfortunately, I have no experience with sphinx yet. But as soon as get it I will be glad to share it with the community.

  24. Hallo,
    i’m working from 2 weeks with presta 1.6 coming from older versions.

    I think is good but i got a problem.
    I’m preparing a site to sell toner and inkjet cartridges.

    I index all the products and i got no error.

    I make an example to explain what’s happens:
    i got a product that hasi in description “TONER CARTRIDGE FOR HP 1010 – OEM CODE Q2612A”

    if i search for 1010 i find it
    if i search for q2612a i find it
    if i search for 2612 no result

    It works in “like” mode but it seems that it wants a complete word. This is a big problem becaouse I can’t be sure about the customer will search for.

    Can anybody help?

    Tnx
    Luca

  25. Luca,

    look for sql request in Search::find() method and you’ll see that request looks like LIKE ‘2612%’, but you need change it to LIKE ‘%2612%’.

  26. Hi Alex,

    Thank you for the information.
    Do you know if it’s possible to show results according to every category selected in the product?
    Now it shows only products for their “default category” but not for all the categories selected.

    For example I have a subcategory called “mamparas abatibles” with 2 products but if I write “mamparas abatibles” in the search box doesnt show any result because these two products have another “default category”.

    Thank you very much!

  27. Hello Alex, has there been an update to this post on July 8: “In a few days I will post an article and will attach a module to include ‘supplier name’ into search.”
    In addition, is there a module to search the CMS pages? Thanks!

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *