I do search in a forum.

I have already done a search for an exact phrase, regardless of the register, but now I want to make a search for a sentence, but not entirely, but, say, by words.

That is, if I make a search request “Chamomiles hid”, then such text from the database should go: “Chamomiles were blooming, hid behind them”.

How to implement it. Through the same array?

  • 3
    And then the array will help you? You decided to do this completely using php, pulling all the articles out of the database into memory and then searching in them? If we already build bicycles, then at least make an index according to the words in the database and search directly in the database without using arrays and php - Mike

1 answer 1

Options:

Use ready solution

(elastic search, sphinx, lucene ... google it):

Write yourself for fun:

In a simplified form, all search engines consist of 2 parts.

Parser / indexer - which will go through the text and make the index.

As an option you take the sentence you break into words. You count how much each word meets. Quantity will be your weight. The more common the word, the closer to the top it should be returned.

Саздаёшь таблицы: words - id - word search_index - word_id - article_id - weight 

Search by index (search_index) You parse the query and search through the tables according to the weight.

  • 2
    If you can create a table with a search index in mysql, with the myasm engine that uses a full-text index. after which we should search for this table and display the posts corresponding to it ... But it is better to take sphinx - Naumov
  • one
    When querying from several words, it is also worth considering the distance between words in the text - Mike
  • @Naumov Actually this is option 1 - E_p
  • 2
    @E_p so it can go to the invention of the search engine)) oftop - Naumov
  • one
    @Naumov InnoDB in mysql 5.6 quietly uses full-text search, no worse than myisam - Alexey Shimansky