Вес мужчины search php keywords. Нужно ли заполнять keywords

У меня уже несколько раз просили написать статью о том, как реализовать поиск на сайте через PHP . Задача это непростая, я бы даже сказал - очень непростая, поскольку здесь имеется огромное количество нюансов и препятствий. В этой статье я разберу алгоритм поиска на сайте .

Давайте предположим, что на нашем сайте имеется множество различных материалов (статей, новостей, заметок и прочего). Всё это добро находится в базе данных. И наша задача - реализовать поиск на сайте . Простейший алгоритм следующий:

  1. Создать HTML-форму со строкой поиска, а также кнопкой "Submit ". В текстовое поле пользователи будут вводить поисковый запрос, а далее нажимать на кнопку.
  2. Получить поисковый запрос (как правило, передаваемый методом GET , но иногда применяют и POST ), а также, в целях защиты от XSS , пропустить его через функцию htmlspecialchars() .
  3. Сделать выборку из соответствующих таблицы (со статьями, новостями, заметками и прочим) тех записей, в которых содержится поисковый запрос. Показываю примерный SQL-запрос для таких случаев: SELECT * FROM articles WHERE `text_article` LIKE %search% Соответственно, вместо search подставляется строка поиска.
  4. Получив записи, в нужном виде выводим их, желательно, по релевантности. Я, например, сделал у себя на сайте так: где больше всего совпадений - та статья и релевантнее, следовательно, ставлю её первой. Скорее всего, Вам этот способ оценки релевантности тоже подойдёт.

Многие из Вас скажут, что ничего сложного здесь нет. И будут отчасти правы, однако, давайте разберём такой пример строки поиска: "ищу этот текст ". Встаёт вопрос: "А что, собственно, ищется? ". То ли ищется точное вхождение текста "ищу этот текст ". Или, быть может, ищется текст, где присутствуют все три слова, но которые могут следовать далеко не друг за другом. Или, возможно, ищется текст, где присутствует хотя бы одно из этих слов.

И вот здесь задача значительно усложняется. Можно сделать сложную систему синтаксиса (как в поисковых системах), например, ищется точное вхождение, если запрос задан в кавычках. А можно давать выбор пользователям, как именно они хотят проводить поиск (с помощью radio-кнопок). Таким образом, сделано у меня на сайте. Поэтому в предыдущий алгоритм добавляется ещё один пункт: составление SQL-запрос . Вот пример SQL-запроса, когда нужно вытащить все материалы, в которых имеется хотя бы одно слово из запроса "ищу этот текст ":

SELECT * FROM articles WHERE (`text_article` LIKE "%ищу%" OR `text_article` LIKE "%этот%" OR `text_article` LIKE "%текст%")

Соответственно, в скрипте поиска Вы должны генерировать подобные SQL-запросы , посылать к базе данных, получать ответ и выводить его. Это всё ещё больше усложняется, если Вы выводите записи по релевантности, так как трудно сразу сказать, что должно быть релевантнее: 3 точных вхождения запроса, либо 10 вхождений частей запроса. У меня на сайте предпочтение всегда отдаётся точным вхожденияи, но этот момент уже достаточно спорен. Безусловно, это сложно, и если это Вы делаете в первый раз, то несколько часов Вы точно потратите. Надеюсь, что мой алгоритм реализации поиска на сайте через PHP Вам поможет.

I have title (varchar), description (text), keywords (varchar) fields in my mysql table.

I kept keywords field as I thought I would be searching in this field only. But I now require to search among all three fields. so for keywords "word1 word2 word3", my query becomes

SELECT * FROM myTable WHERE (name LIKE "%word1%" OR description LIKE "%word1%" OR keywords LIKE "%word1%" OR name LIKE "%word2%" OR description LIKE "%word2%" OR keywords LIKE "%word2%" OR name LIKE "%word3%" OR description LIKE "%word3%" OR keywords LIKE "%word3%") AND status = "live"

Looks a bit messy but this works. But now I need to implement synonym search. so for a given word assuming there are a few synonyms available this query becomes more messy as I loop through all of the words. As the requirements are getting clearer, I will need to join this myTable to some other tables as well.

    Do you think the above way is messy and will cause problems as the data grow?

    How can I avoid above mess? Is there any cleaner solution I can go by? Any example will help me.

  • Is there any other method/technique you can recommend to me?

EDIT

@Peter Stuifzand suggested me that I could create one search_index table and store all 3 fields (title,keyword,desc) info on that and do full text search. I understand that additionally this table will include reference to myTable primary key as well.

But my advanced search may include joining mytable with Category table, geographic_location table (for searching within 10, 20 miles etc), filtering by someother criteria and of course, sorting of search results. Do you think using mysql fulltext will not slow it down?

We help a variety of clients with their internet marketing and websites, and one question we often get is “How do you add keywords to a website?” You might picture us adding extremely complicated formulas and codes into a computer screen.

But the truth is the basics are easier than you might think. We even teach our clients that manage their own business blogs how to keyword their website pages so that they can be found on search engines easier. The goal of this blog article is to teach you some fundamentals on how to add keywords to a website. Not sure you wan"t to put in the time? Take a look at our SEO Services here, we would be happy to help you.

Why Should You Know How to Add Keywords to a Website?

By learning how to add keywords to a website you will be able to keyword your own blogs, website pages, and other internet marketing materials. You will also gain context for why SEO is so important for your business.

How Can Adding Keywords to My Website Help My Business?

Adding keywords to your website helps search engines understand what your website can offer someone searching, and ultimately bring you more qualified traffic. How? With identifiers, like keywords. Without Keywords on your website pages there is no way for a search engine to categorize your website and show it to the right people searching .Think of it this way, a well written paper has a thesis, and supporting arguments that relate to the thesis. Readers of well written papers have a clear understanding of what the subject is and what the paper is about. This is the same theory behind Google and other search engines. In fact two students from Stanford created Google with this same idea in mind.

A well made website has a main subject, and often has sub categories that relate to the main subject, and by keywording each of these areas we are able to give a clear picture to search engines, and people searching are able to find you more easily. For example:

Your Main Subject Might Be: Doughnuts

Your Sub-Categories or Topics Could Be: Cake, Dougnut Holes, Bars,…

Choosing Keywords for Your Website

Now that you understand the framework of a site and how it matters it’s time to choose keywords. How? We use a number of tools and perform extensive research for our keywords; however one tool that is free is the Google Keyword Tool. Simply input your location information and category, then type in the main subject of your website. The tool will generate a number of keywords, and give you stats like these:

Competition: (Low, Med, High) This tells you how many people are trying to keyword for that word or phrase. The higher the competition the more difficult it is to rank high in Google for that search term.

Global Monthly Volume: How many searches are made per month globally for that term.

Local Monthly: Is determined by the information you put into your search. If you specified your location as the U.S. then it would be the number of monthly searches for that term in the U.S.

While this tool is easy to use, all keywords are not treated the same. In fact some keywords bring you more traffic than others, and some might bring you a lot of traffic that never converts. This is why we highly recommend business owners hiring an agency that is educated in SEO and keywording to help them with their internet marketing.

How Many Keywords Do I Need to Add Per Page?

After you have conducted your keyword research you will need to choose one keyword for each of your website pages or blogs. Keywords should be specific to the page topic and relate to your overall website subject.

Example of Good Keyword Choice:

Main Website Theme: Donut

Website Page: Maple Donut

Assigned Keyword: Best Maple Donut

Example of Bad Keyword Choice:

Main Website Theme: Donut

Website Page: Maple Donut

Assigned Keyword: Donut recipes

How to Add Keywords to Your Website Page:

When adding keywords to your website, it is important to include your keyword in 6 places on each page of your website. Including your keyword in these 6 areas will help search engines identify the subject of your page and rank your page in search results.

  1. Page Title
  2. Meta Description
  3. Header
  4. Sub Header
  5. Body Paragraphs
  6. Image Alt Tags

Page Title & Meta Description:

Page Titles & Meta Descriptions are a more technical part of keywording your website. However, it is important to recognize how valuable they can be for your internet marketing efforts. What are Page Titles & Meta Descriptions? These parts of your website page actually show up in search results, they are the first impression a searcher gets of your website page.

If you do not have access to your website Page Titles or Meta Descriptions then it will be important to check with your website management company that those areas are filled out correctly for SEO.

Headers:

Headers are a lot like billboards for search engines. They are one of the biggest ways to show search engines what your main subject is for your page. It is important that you include your entire keyword in your header.

Sub-Headers:

Sub-headers are another area to tell search engines what you want the website page to be found for. Think of this area as real-estate, if you don’t try to include your keywords in the sub-header then you are missing out.

Body Paragraph:

When writing the body content for your website page you should try to include your keyword, or at least parts of your keyword. Remember to keep your writing natural, search engines will actually penalize you if your writing over stuffs keywords and appears unnatural. When you first try to write with keywords you might find it difficult, but keep practicing! It really does get easier, and you will get better at shaping your content for adding keywords.

Image Alt Tags:

Images are a great addition to any webpage, in fact they can even help search engines rank you. Alt tags are essentially a label that you assign to your image so that search engines can read the image. If you don’t use Alt tags for images then search engines will not see it. By keywording these image Alt Tags you are telling search engines "I used a picture and it relates to the subject of my page."

There you have it, now you know the basics of how to add keywords to a website. We know it looks daunting, but if you have the time to write your own blogs or website content, then we highly recommend you use some of the tips listed above. These tactics can bring you more traffic to your website, and help qualify your website leads. That means no wasted visits and more customers for your business.

If you found this article helpful in anyway please ‘share’ it with a friend.

By Ibrahim Diallo

Published Jul 2 2014 ~ 16 minutes read

Search is an important feature on a website. When my few readers want to look for a particular passage on my blog, they use the search box. It used to be powered by Google Search, but I have since then changed it to my own home-brewed version not because I can do better but because it was an interesting challenge.

If you are in a hurry and just want your site to be searchable, well do what I did before, use Google.

// In search.php file $term = isset($_GET["query"])?$_GET["query"]: ""; $term = urlencode($term); $website = urlencode("www.yourwebsite.com"); $redirect = "https://www.google.com/search?q=site%3A{$website}+{$term}"; header("Location: $redirect"); exit;

What it does is pretty simple. Get the term passed by the user, and forward it to Google search page. Limit the search result to our current domain using the site: keyword in the search query. All your pages that are indexed by Google will be available through search now. If you do want to handle your search in house however, then keep reading.

Homemade Search Solution

Before we go any further, try using the search box on this blog. It uses the same process that I will describe below. If you feel that this is what you want then please continue reading.

This solution is catered to small websites. I make use of LIKE with wild cards on both ends, meaning your search cannot be indexed. This means the solution will work fine for your blog or personal website that doesn"t contain tons of data. Port it to a bigger website and it might become very slow. MySQL offers Full Text Search which is not what we are doing here.

Note: If you have 5000 blog posts you are still fine. .

We will take the structure of this blog as a reference. Each blog post has:

  • A title p_title
  • A url p_url
  • A summary p_summary
  • A post content p_content
  • And catergories category.tagname

For every field that matches with our search term, we will give it a score. The score will be based on the importance of the match:

// the exact term matches is found in the title $scoreFullTitle = 6; // match the title in part $scoreTitleKeyword = 5; // the exact term matches is found in the summary $scoreFullSummary = 5; // match the summary in part $scoreSummaryKeyword = 4; // the exact term matches is found in the content $scoreFullDocument = 4; // match the document in part $scoreDocumentKeyword = 3; // matches a category $scoreCategoryKeyword = 2; // matches the url $scoreUrlKeyword = 1;

Before we get started, there are a few words that do not contribute much to a search that should be removed. Example "in","it","a","the","of" ... . We will filter those out and feel free to add any word you think is irrelevant. Another thing is, we want to limit the length of our query. We don"t want a user to write a novel in the search field and crash our MySQL server.

// Remove unnecessary words from the search term and return them as an array function filterSearchKeys($query){ $query = trim(preg_replace("/(\s+)+/", " ", $query)); $words = array(); // expand this list with your words. $list = array("in","it","a","the","of","or","I","you","he","me","us","they","she","to","but","that","this","those","then"); $c = 0; foreach(explode(" ", $query) as $key){ if (in_array($key, $list)){ continue; } $words = $key; if ($c >= 15){ break; } $c++; } return $words; } // limit words number of characters function limitChars($query, $limit = 200){ return substr($query, 0,$limit); }

Our helper functions can now limit character count and filter useless words. The way we will implement our algorithm is by giving a score every time we find a match. We will match words using the if statement and accumulate points as we match more words. At the end we can use that score to sort our results

Note: I will not be showing how to connect to MySQL database. If you are having problems to efficiently connect to the database I recommend reading this .

Let"s give our function a structure first. Note I left placeholders so we can implement sections separately.

Function search($query){ $query = trim($query); if (mb_strlen($query)===0){ // no need for empty search right? return false; } $query = limitChars($query); // Weighing scores $scoreFullTitle = 6; $scoreTitleKeyword = 5; $scoreFullSummary = 5; $scoreSummaryKeyword = 4; $scoreFullDocument = 4; $scoreDocumentKeyword = 3; $scoreCategoryKeyword = 2; $scoreUrlKeyword = 1; $keywords = filterSearchKeys($query); $escQuery = DB::escape($query); // see note above to get db object $titleSQL = array(); $sumSQL = array(); $docSQL = array(); $categorySQL = array(); $urlSQL = array(); /** Matching full occurrences PLACE HOLDER **/ /** Matching Keywords PLACE HOLDER **/ $sql = "SELECT p.p_id,p.p_title,p.p_date_published,p.p_url, p.p_summary,p.p_content,p.thumbnail, ((-- Title score ".implode(" + ", $titleSQL).")+ (-- Summary ".implode(" + ", $sumSQL).")+ (-- document ".implode(" + ", $docSQL).")+ (-- tag/category ".implode(" + ", $categorySQL).")+ (-- url ".implode(" + ", $urlSQL).")) as relevance FROM post p WHERE p.status = "published" HAVING relevance >

In the query, all scores will be summed up as the relevance variable and we can use it to sort the results.

Matching full occurrences

We make sure we have some keywords first then add our query.

If (count($keywords) > 1){ $titleSQL = "if (p_title LIKE "%".$escQuery."%",{$scoreFullTitle},0)"; $sumSQL = "if (p_summary LIKE "%".$escQuery."%",{$scoreFullSummary},0)"; $docSQL = "if (p_content LIKE "%".$escQuery."%",{$scoreFullDocument},0)"; }

Those are the matches with higher score. If the search term matches an article that contains these, they will have higher chances of appearing on top.

Matching keywords occurrences

We loop through all keywords and check if they match any of the fields. For the category match, I used a sub-query since a post can have multiple categories.

Foreach($keywords as $key){ $titleSQL = "if (p_title LIKE "%".DB::escape($key)."%",{$scoreTitleKeyword},0)"; $sumSQL = "if (p_summary LIKE "%".DB::escape($key)."%",{$scoreSummaryKeyword},0)"; $docSQL = "if (p_content LIKE "%".DB::escape($key)."%",{$scoreDocumentKeyword},0)"; $urlSQL = "if (p_url LIKE "%".DB::escape($key)."%",{$scoreUrlKeyword},0)"; $categorySQL = "if ((SELECT count(category.tag_id) FROM category JOIN post_category ON post_category.tag_id = category.tag_id WHERE post_category.post_id = p.post_id AND category.name = "".DB::escape($key)."") > 0,{$scoreCategoryKeyword},0)"; }

Also as pointed by a commenter below, we have to make sure that the these variables are not empty arrays or the query will fail.

// Just incase it"s empty, add 0 if (empty($titleSQL)){ $titleSQL = 0; } if (empty($sumSQL)){ $sumSQL = 0; } if (empty($docSQL)){ $docSQL = 0; } if (empty($urlSQL)){ $urlSQL = 0; } if (empty($tagSQL)){ $tagSQL = 0; }

At the end the queries are all concatenated and added together to determine the relevance of the post to the search term.

// Remove unnecessary words from the search term and return them as an array function filterSearchKeys($query){ $query = trim(preg_replace("/(\s+)+/", " ", $query)); $words = array(); // expand this list with your words. $list = array("in","it","a","the","of","or","I","you","he","me","us","they","she","to","but","that","this","those","then"); $c = 0; foreach(explode(" ", $query) as $key){ if (in_array($key, $list)){ continue; } $words = $key; if ($c >= 15){ break; } $c++; } return $words; } // limit words number of characters function limitChars($query, $limit = 200){ return substr($query, 0,$limit); } function search($query){ $query = trim($query); if (mb_strlen($query)===0){ // no need for empty search right? return false; } $query = limitChars($query); // Weighing scores $scoreFullTitle = 6; $scoreTitleKeyword = 5; $scoreFullSummary = 5; $scoreSummaryKeyword = 4; $scoreFullDocument = 4; $scoreDocumentKeyword = 3; $scoreCategoryKeyword = 2; $scoreUrlKeyword = 1; $keywords = filterSearchKeys($query); $escQuery = DB::escape($query); // see note above to get db object $titleSQL = array(); $sumSQL = array(); $docSQL = array(); $categorySQL = array(); $urlSQL = array(); /** Matching full occurences **/ if (count($keywords) > 1){ $titleSQL = "if (p_title LIKE "%".$escQuery."%",{$scoreFullTitle},0)"; $sumSQL = "if (p_summary LIKE "%".$escQuery."%",{$scoreFullSummary},0)"; $docSQL = "if (p_content LIKE "%".$escQuery."%",{$scoreFullDocument},0)"; } /** Matching Keywords **/ foreach($keywords as $key){ $titleSQL = "if (p_title LIKE "%".DB::escape($key)."%",{$scoreTitleKeyword},0)"; $sumSQL = "if (p_summary LIKE "%".DB::escape($key)."%",{$scoreSummaryKeyword},0)"; $docSQL = "if (p_content LIKE "%".DB::escape($key)."%",{$scoreDocumentKeyword},0)"; $urlSQL = "if (p_url LIKE "%".DB::escape($key)."%",{$scoreUrlKeyword},0)"; $categorySQL = "if ((SELECT count(category.tag_id) FROM category JOIN post_category ON post_category.tag_id = category.tag_id WHERE post_category.post_id = p.post_id AND category.name = "".DB::escape($key)."") > 0,{$scoreCategoryKeyword},0)"; } // Just incase it"s empty, add 0 if (empty($titleSQL)){ $titleSQL = 0; } if (empty($sumSQL)){ $sumSQL = 0; } if (empty($docSQL)){ $docSQL = 0; } if (empty($urlSQL)){ $urlSQL = 0; } if (empty($tagSQL)){ $tagSQL = 0; } $sql = "SELECT p.p_id,p.p_title,p.p_date_published,p.p_url, p.p_summary,p.p_content,p.thumbnail, ((-- Title score ".implode(" + ", $titleSQL).")+ (-- Summary ".implode(" + ", $sumSQL).")+ (-- document ".implode(" + ", $docSQL).")+ (-- tag/category ".implode(" + ", $categorySQL).")+ (-- url ".implode(" + ", $urlSQL).")) as relevance FROM post p WHERE p.status = "published" HAVING relevance > 0 ORDER BY relevance DESC,p.page_views DESC LIMIT 25"; $results = DB::query($sql); if (!$results){ return false; } return $results; }

Now your search.php file can look like this:

$term = isset($_GET["query"])?$_GET["query"]: ""; $search_results = search($term); if (!$search_results) { echo "No results"; exit; } // Print page with results here.

We created a simple search algorithm that can handle a fair amount of content. I arbitrarily chose the score for each match, feel free to tweak it to something that works best for you. And there is always room for improvement.

It is a good idea to track the search term coming from your users, this way you can see if most users search for the same thing. If there is a pattern, then you can save them a trip and just cache the results using Memcached .

If you want to see this search algorithm in action, go ahead and try looking for an article on the search box on top of the page. I have added extra features like returning the part where the match was found in the text. Feel free to add features to yours.

Did you like this article? You can subscribe to read more awesome ones. .

On a related note, here are some interesting articles.

It is time to deal with mysql_* functions once and for all. These methods are deprecated and slow. The time to upgrade has long passed yet we still see it everywhere. Since I cannot force every author to update their tutorial and blogs, I decided to write a post to hopefully rank better and provide the essential information to help new comers.

Making your own website shouldn"t be too difficult. Hosting companies like Godaddy or Hostgator make it super easy for anyone to get started; they allow you to create a whole website without ever writing code. For most people, it is plenty to run a WordPress blog. If this is what you are looking for you should head to Godaddy.com right now. We are done here. But on the other hand, if you want to have control and not be limited by the short comings of a shared hosting without busting your wallet, you have come to the right place.

Vim is my favorite text editor on the terminal. After playing for a little while with nano and emacs , I finally settled with vim for its simplicity (bare with me please). Although it can be customized and used like an entire IDE, I use it mostly for editing files on my servers and making small but crucial changes. Let"s not get into Editor war and get started.

Comments(45)

Zaryel Aug 12 2015:

Ian Mustafa Sep 26 2015:

Rob Sep 29 2015:

adeem Feb 11 2016:

Ivan Venediktov Apr 9 2016.

При работе автоматических парсеров, возникает естественная необходимость подобрать к тексту его ключевые слова. Это необходимо для поисковой оптимизации статьи.

К примеру мы спарсили объявление с доски, в нем продается Apple Iphone 4s, подержанный. Небольшой текст описания с характеристиками и цена.

Из этого текста необходимо выделить ключевые слова. Заполнять их при автоматическом парсинге будет накладно. Поэтому нужно использовать скрипты для выборки ключевых слов.

Для начала, нужно составить список всех слов употребляемых в тексте, с сортировкой по частоте их употребления. Откинув короткие слова (меньше 5-ти знаков) и различные вспомогательные части речи, типа местоимений и наречий.

Когда-то давно, для разработки сайтов я использовал Danneo CMS. Так вот, в ней была великолепная функция выделения ключевых слов в тексте. Только ради этого, скачал новую версию Danneo, она кстати обновилась до версии 0.5.5

Вот эта функиця

Function seokeywords($contents,$symbol=5,$words=35){ $contents = @preg_replace(array(""<[\/\!]*?[^<>]*?>"si",""([\r\n])[\s]+"si",""&{1,6};"si",""(+)"si"), array("","\\1 "," "," "),strip_tags($contents)); $rearray = array("~","!","@","#","$","%","^","&","*","(",")","_","+", "`",""","№",";",":","?","-","=","|","\"","\\","/", "[","]","{","}",""",",",".","<",">","\r\n","\n","\t","«","»"); $adjectivearray = array("ые","ое","ие","ий","ая","ый","ой","ми","ых","ее","ую","их","ым", "как","для","что","или","это","этих", "всех","вас","они","оно","еще","когда", "где","эта","лишь","уже","вам","нет", "если","надо","все","так","его","чем", "при","даже","мне","есть","только","очень", "сейчас","точно","обычно"); $contents = @str_replace($rearray," ",$contents); $keywordcache = @explode(" ",$contents); $rearray = array(); foreach($keywordcache as $word){ if(strlen($word)>=$symbol && !is_numeric($word)){ $adjective = substr($word,-2); if(!in_array($adjective,$adjectivearray) && !in_array($word,$adjectivearray)){ $rearray[$word] = (array_key_exists($word,$rearray)) ? ($rearray[$word] + 1) : 1; } } } @arsort($rearray); $keywordcache = @array_slice($rearray,0,$words); $keywords = ""; foreach($keywordcache as $word=>$count){ $keywords.= ",".$word; } return substr($keywords,1); }

из выше набранного текста функция выдает вот такой набор

Array,rearray,contents,keywordcache,слова,keywords,слов,adjectivearray,symbol,replace,words,этого,вот,

Danneo,тексте,функция,нужно,ключевых,foreach,текста,ключевые,substr,необходимо,adjective,выдает,

была,великолепная,выделения,скачал,ради,Только,ней,набранного,набор,давно

не плохо ни правда ли?!

Дальше, можно поступить следующим образом: просеять все эти слова по базе заведомо подготовленных ключевиков. И оставить только нужные. У меня такая база создавалась несколько лет вручную. Это база тегов, которыми подкрепляется каждая статья.

Так по ключевым словам можно легко восстановить информацию о статье. Т.е. поисковик будет иметь конкретное представление о том, что содержим Ваша страница. Подобно тому, как