wordpress搜索优化_改善本机WordPress搜索

默认的WordPress搜索在处理大量内容时表现不佳,查询时间过长,尤其对高流量网站来说。本文探讨了WordPress搜索的工作原理,指出其局限性,如仅搜索特定字段、不适用于自定义帖子类型等。提出了一些改进方案,如使用Algolia、Elasticsearch、Amazon CloudSearch、Relevanssi和SearchWP等,以提高搜索性能和相关性,同时提供了选择正确搜索解决方案的依据。
摘要由CSDN通过智能技术生成

wordpress搜索优化

This article on WordPress search was originally published by Torque Magazine, and is reproduced here with permission.

这篇有关WordPress搜索的文章最初由Torque Magazine发行 ,并经许可在此处转载。

If you have ever worked on a WordPress site with a massive amount of posts and pages, you might have noticed something about the WordPress search: it doesn’t perform well. In this article, we dive into why the default WordPress search doesn’t scale and some ways to make it better.

如果您曾经在拥有大量帖子和页面的WordPress网站上工作,则可能已经注意到有关WordPress搜索的一些信息:它的效果不佳。 在本文中,我们将深入探讨默认WordPress搜索无法扩展的原因以及一些使其变得更好的方法。

WordPress如何搜寻 (How WordPress Searches)

By default, the native WordPress search performs a query on your database that looks something like this:

默认情况下,本机WordPress搜索对您的数据库执行如下查询:

A native WordPress database search query

Let’s break down this query a little bit.

让我们分解一下这个查询。

  • First, we are using SQL_CALC_FOUND_ROWS on the wp_posts table. This says, if there were no limit (coming later), how many results would the following query turn up?

    首先,我们在wp_posts表上使用SQL_CALC_FOUND_ROWS 。 这就是说,如果没有限制(稍后再来),以下查询将显示多少个结果

  • Then we search through the post_title, post_excerpt, and post_content columns for the search term (in this example I searched “tech”).

    然后,我们在post_titlepost_excerptpost_content列中搜索搜索词(在本示例中,我搜索了“ tech”)。

  • And we ensure that the content returned is a post, page, or attachment (media).

    并且我们确保返回的内容是帖子,页面或附件(媒体)。
  • We check to make sure the status is “public”–not deleted or private (if you are a logged-in user it will also search only your own private posts).

    我们检查以确保状态为“公开”-不删除或不公开(如果您是登录用户,它还将仅搜索您自己的不公开帖子)。
  • We order the results by matching title, descending, or post date, descending.

    我们通过匹配标题(降序)或发布日期(降序)来对结果进行排序。
  • Last, we limit the results to 10, paginating the rest of the results.

    最后,我们将结果限制为10,对其余结果进行分页。

That’s a whole lot of checks WordPress runs through just to return search results. And every time you iterate through wp_posts to perform each check, MySQL is processing all rows in wp_posts. When you have hundreds of thousands of rows in the wp_posts table, this query can get ugly really quick. Here are some benchmarks from a few of my sites:

这是WordPress为了返回搜索结果而进行的大量检查。 每次您遍历wp_posts进行每次检查时,MySQL都会处理wp_posts中的所有行。 当wp_posts表中有成千上万的行时,此查询很快就会变得很丑陋。 以下是我一些站点的一些基准:

Bar graph showing query times per number of posts

On sites with a very large number of posts, the query took over 20 seconds! This is not a scalable search option for sites with high traffic.

在发布大量信息的网站上,查询耗时超过20秒! 对于流量较高的网站,这不是可扩展的搜索选项。

问题识别 (Problem Identification)

Knowing what we now know about the default search behavior, it’s important to call out why this might not be ideal for some websites.

了解我们现在对默认搜索行为的了解后,重要的是要指出为什么这对于某些网站可能不理想。

  • WordPress only searches the “post_title,” “post_content,” and “post_excerpt” fields for your search terms. It also only presents “post,” “page,” and “attachment” post types. For users with custom post types, custom fields, or plugins like WooCommerce that add different page/post types, this can be problematic: these items will not appear in search results.

    WordPress仅在“ post_title”,“ post_content”和“ post_excerpt”字段中搜索您的搜索词。 它还仅显示“帖子”,“页面”和“附件”帖子类型。 对于具有自定义帖子类型,自定义字段或添加了不同页面/帖子类型的插件(如WooCommerce)的用户,这可能会出现问题:这些项目不会出现在搜索结果中。
  • The query used by WordPress search performs very poorly at over 100,000 posts. For news sites or media sites with more than 100,000 posts, a search could take several seconds to perform.

    WordPress搜索使用的查询在100,000多个帖子中的表现非常差。 对于帖子数超过100,000的新闻网站或媒体网站,搜索可能需要花费几秒钟的时间。
  • The poor performance on the search query could cause server performance issues if your site receives a fair amount of search traffic.

    如果您的站点收到大量搜索流量,则搜索查询的性能不佳可能会导致服务器性能问题。

在WordPress本地搜索上进行改进 (Improving on WordPress Native Search)

So now that we know WordPress native search isn’t an option for many sites, we can explore search solutions. Based on our problem identification above, our qualifications for a search solution include:

因此,既然我们知道WordPress本机搜索不是许多站点的选择,那么我们可以探索搜索解决方案。 根据上面的问题识别,我们对于搜索解决方案的资格包括:

  • Search tools that will search all content or customized sets of content, not limited to the post types or post fields defined by default.

    搜索工具将搜索所有内容或自定义的内容集,不限于默认情况下定义的帖子类型或帖子字段。
  • Search tools that perform well when presented with large data sets.

    呈现大数据集时,搜索工具的效果很好。
  • Search tools that will not cause poor server performance when presented with high traffic.

    搜索工具在出现高流量时不会导致服务器性能下降。

Below we will explore several solutions, including enterprise-grade external services and WordPress plugins.

下面我们将探讨几种解决方案,包括企业级外部服务和WordPress插件。

阿尔及利亚搜索 (Algolia Search)

Algolia is an enterprise-grade search solution in which your posts and content are indexed offsite and returns results to your users. Its feature set includes fuzzy search, geolocation search, multi-language support, and search with synonyms.

Algolia是一种企业级搜索解决方案,其中,您的帖子和内容在异地索引,并将结果返回给用户。 其功能集包括模糊搜索,地理位置搜索,多语言支持以及同义词搜索。

Comparing search performance on a site with thousands of posts, there are some clear benefits with Algolia. Below is the performance of the default WordPress search:

比较具有数千个帖子的网站上的搜索效果,Algolia有一些明显的好处。 以下是默认WordPress搜索的性能:

DevTool view of the default WordPress search performance

Notice the search took 7.43 seconds using the default WordPress search. However, after implementing Algolia search the results were nearly instant:

请注意,使用默认的WordPress搜索花费了7.43秒的搜索时间。 但是,在实施Algolia搜索之后,结果几乎是即时的:

DevTool view of Algolia search performance

The Algolia results were about 15x faster than standard WordPress search! That is a massive improvement in performance. Not to mention, if the site supports a large amount of traffic with concurrent searches, it will not cause strain on the server. Offloading searches to an external service that is specifically optimized for searching is a big win.

Algolia的搜索结果比标准WordPress搜索快15倍! 这是性能的巨大提高。 更不用说,如果该站点通过并发搜索支持大量流量,则不会对服务器造成压力。 将搜索卸载到专门针对搜索优化的外部服务上是一个巨大的胜利。

弹性搜索 (Elasticsearch)

ElasticPress is a product made by Elastic, which offloads your searching to your own Elasticsearch server instance. This search tool is unique in that you can choose where to host your Elasticsearch instance: your local desktop, dedicated servers, or even on the same server where your WordPress website is hosted. And with the ElasticPress plugin, you can easily connect this instance to your website for use.

ElasticPress是Elastic的产品,它将您的搜索工作转移到您自己的Elasticsearch服务器实例中。 该搜索工具的独特之处在于,您可以选择托管Elasticsearch实例的位置:本地桌面,专用服务器,甚至位于托管WordPress网站的同一服务器上。 借助ElasticPress插件 ,您可以轻松地将此实例连接到您的网站以供使用。

ElasticPress + ElasticSearch is a winning combination that provides excellent search features like:

ElasticPress + ElasticSearch是一个成功的组合,可提供出色的搜索功能,例如:

  • Including WooCommerce products in search results

    在搜索结果中包括WooCommerce产品
  • Display related posts without expensive, slow queries

    显示相关帖子,而无需进行昂贵而缓慢的查询
  • Choose to index all, some, or only select content on your website

    选择索引所有,部分或仅选择网站上的内容
  • Automatically complete search suggestions as users type

    用户键入时自动完成搜索建议

Because the search is offloaded to a service optimized for search, the performance impact for sites with many thousands of posts is boosted as well.

由于将搜索转移到针对搜索进行了优化的服务,因此,对于具有数千个帖子的网站的性能影响也得到了增强。

亚马逊云搜索 (Amazon CloudSearch)

CloudSearch

The CloudSearch service from Amazon is a viable option in terms of power, performance, and scalability. Amazon uses a “pay-as-you-go” payment service and this makes CloudSearch an inexpensive enterprise-grade solution. You can customize which items get indexed in the setup process as well, but the learning curve for this product is steep. While some like Lift have created WordPress plugins integrating CloudSearch, they are not widely used or often updated, and may not be as reliable. This means that chances are you will be developing your own plugin implementation of Amazon CloudSearch. You can use their Developer Guide for CloudSearch to get started.

就功能,性能和可扩展性而言,Amazon的CloudSearch服务是一个可行的选择。 亚马逊使用“现收现付”支付服务,这使CloudSearch成为一种廉价的企业级解决方案。 您也可以在设置过程中自定义要索引的项目,但是该产品的学习曲线很陡。 尽管有些类似Lift的公司创建了集成了CloudSearch的WordPress插件,但它们并未得到广泛使用或经常更新,因此可能不那么可靠。 这意味着您将有可能开发自己的Amazon CloudSearch插件实现。 您可以使用他们的CloudSearch开发人员指南开始使用。

相关性 (Relevanssi)

Relevanssi

Relevanssi is a WordPress plugin that optimizes the native WordPress search “in-house.” That is, it searches your own database on your own server, just with better results. Some of the key benefits this plugin offers, compared to the default WordPress search:

Relevanssi是一个WordPress插件,可优化“内部”本地WordPress搜索。 也就是说,它会在您自己的服务器上搜索自己的数据库,从而获得更好的结果。 与默认的WordPress搜索相比,此插件提供的一些关键优势:

  • Fuzzy searches (include partial matches)

    模糊搜索(包括部分匹配)
  • Sort by relevance, not by date

    按相关性排序,而不按日期排序
  • Select whether the results should match all words, or just one of the words

    选择结果应匹配所有单词还是仅匹配其中一个单词
  • Search all content including comments, categories, tags, and custom fields

    搜索所有内容,包括评论,类别,标签和自定义字段
  • Excerpts display and highlight where the match occurred for context

    摘录显示并突出显示上下文匹配的位置

The one aspect Relevanssi does not necessarily improve upon is the speed or scalability of the search function. In this way, Relevanssi is ideal for users who are frustrated with the type of results WordPress search produces. It is not ideal for users who are seeking performance improvements in search.

Relevanssi不一定要改进的一个方面是搜索功能的速度或可伸缩性。 这样,Relevanssi是对WordPress搜索产生的结果类型感到沮丧的用户的理想选择。 对于寻求提高搜索性能的用户而言,它不是理想的选择。

搜索WP (SearchWP)

Last but not least, SearchWP is another paid search solution for WordPress. As with Relevanssi, this plugin extends the search to include better results but makes no claim to improve search performance. SearchWP has extensive configuration options to include exactly what you want in search results. Search things like: eCommerce products, PDFs and Media, Custom Posts and Fields, Categories and Tags, and Shortcode output. Beyond the additional search vectors, SearchWP offers keyword stemming as well. So if your end user searches for a related keyword, they will get content from related keywords in the results.

最后但并非最不重要的一点, SearchWP是WordPress的另一个付费搜索解决方案。 与Relevanssi一样,此插件将搜索范围扩大到包括更好的结果,但没有声称会提高搜索性能。 SearchWP具有广泛的配置选项,可以准确地在搜索结果中包含您想要的内容。 搜索以下内容:电子商务产品,PDF和媒体,自定义帖子和字段,类别和标签以及简码输出。 除了其他搜索向量之外,SearchWP还提供关键字词干。 因此,如果最终用户搜索相关的关键字,则他们将从结果中的相关关键字获得内容。

SearchWP also goes the extra mile in terms of logging searches and providing insights on search traffic. This can help inform your content strategy in ways that make your content easier to find.

SearchWP在记录搜索并提供有关搜索流量的见解方面也更加出色。 这可以帮助您以更容易查找内容的方式通知您的内容策略。

选择正确的搜索解决方案 (Choosing the Right Search Solution)

In the end, the search solution you choose will depend upon your budget and upon which problems are the highest priority for your team. If performance gains are your primary concern, Algolia and Elasticsearch are the clear winners. However, if you just want to see better search results, Relevanssi and SearchWP are simple yet powerful options. And if you want a cost-effective way to completely customize the way your website searches in a customized plugin, Amazon CloudSearch could be the right solution. You can use the descriptions above to identify your priorities, and the proper solution to fit that need. As always, be sure to test any new integration or plugin in a staging or development environment first! This will allow you the freedom to test your new search implementation in a non-user-impacting way.

最后,您选择的搜索解决方案将取决于您的预算以及哪些问题是团队中最优先考虑的问题。 如果您最关心性能提升,那么Algolia和Elasticsearch无疑是赢家。 但是,如果您只是想查看更好的搜索结果,Relevanssi和SearchWP是简单而强大的选项。 而且,如果您想要一种经济有效的方式来完全自定义网站在自定义插件中的搜索方式,那么Amazon CloudSearch可能是正确的解决方案。 您可以使用上面的描述来确定您的优先级,以及适合此需求的适当解决方案。 与往常一样,请务必先在登台或开发环境中测试任何新的集成或插件! 这将使您能够以不影响用户的方式自由测试新的搜索实现。

翻译自: https://www.sitepoint.com/improving-native-wordpress-search/

wordpress搜索优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值