⬆️ ⬇️

Own search engine on the site

I often encounter such a situation when my own internal full-text search is required on Internet sites. Implementing it is not so difficult, and the effect of its implementation is huge.

So on one of its projects it was necessary to implement a quick search for thematic information from the database. At the moment, to solve such problems, tags and headings to records are used. But in my case it was not an option. The formulation of the problem is as follows: on the basis of the entered two or three words, it is necessary to output records containing these words.

The structure in a simplified form of the database is shown in the figure.



image



The “Words” table contains all the words that are found in the existing records, the “Records” table, respectively, the texts of the records, and the “Links” table includes references to the use of the corresponding words by the records.

All the work of this simplified search engine contains a number of stages. The first step is to add a new entry. So the entire text of the record is divided into an array of words. After that, several queries to the database are executed: it is checked whether these words already exist in the “Words” table, if they are not present, all the necessary records are added. Identifies the identifiers of each word. The final step is to add pairs of word ids and entries to the "Links" table.

Editing is essentially the same as adding a record, except that it is necessary to delete all the records related to the edited text from the table “Links”. With removal, I think, questions will not arise.

Now regarding the search itself records. Suppose a user enters a word in the search field. The search field itself is an input field with self-completion.

Step 1:

')

image



Step 2:



image



Step 3:



image



That is, when you enter the next word, it is checked which of the available words, in the first case, with the letter “c”, are in the records of the “Records” table. In the second step, it is checked which words starting with “ra” are in the entries containing the word “method”. In the third step, the words are searched for “k” in the records, where the words “method” and “allow” already exist. I believe that three words to search is quite enough even among a large number of entries.

After the formed set of words we start the search. Everything is simple here. Identifies the identifiers of words that are entered in the input field, filtered entries in the table "Links", in which there are identifiers of words. From the resulting array, the identifiers of records from the table “Records” are determined for which all the search words are present.



What do we end up with?



In the end, the user has the opportunity to quickly search for the necessary records by his request, while the system itself helps him formulate this very request. In addition, if we talk about the tag system, then the need for it completely disappears. But it is rather a matter of taste.

Source: https://habr.com/ru/post/98505/



All Articles