带有Elasticsearch的Wagtail多站点将搜索结果限制为仅1个站点

时间:2018-11-26 04:34:33

标签: elasticsearch wagtail wagtail-search

当使用多站点时,似乎a页面不知道它属于哪个站点。 当我要控制要搜索的多站点站点数时,这是一个问题。

这是wagtailcore_page表的sql(未提及site_id):

CREATE TABLE "wagtailcore_page" ("id" integer NOT NULL PRIMARY KEY 
AUTOINCREMENT, "path" varchar(255) NOT NULL UNIQUE, "depth" integer unsigned 
NOT NULL, "numchild" integer unsigned NOT NULL, "title"
varchar(255) NOT NULL, "slug" varchar(255) NOT NULL, "live" bool NOT NULL, 
"has_unpublished_changes" bool NOT NULL, "url_path" text NOT NULL, 
"seo_title" varchar(255) NOT NULL, "show_in_menus" bool NOT NULL
 , "search_description" text NOT NULL, "expire_at" datetime NULL, "expired" 
bool NOT NULL, "content_type_id" integer NOT NULL REFERENCES 
"django_content_type" ("id"), "owner_id" integer NULL REFERENCES "auth_
user" ("id"), "locked" bool NOT NULL, "latest_revision_created_at" datetime 
NULL, "first_published_at" datetime NULL, "go_live_at" datetime NULL);

网站仅知道其根页面-wagtailcore_site表:

CREATE TABLE "wagtailcore_site" ("id" integer NOT NULL PRIMARY KEY 
AUTOINCREMENT, "hostname" varchar(255) NOT NULL, "port" integer NOT NULL, 
"is_default_site" bool NOT NULL, "site_name" varchar(255)
NULL, "root_page_id" integer NOT NULL REFERENCES "wagtailcore_page" ("id"));

解决方案:

  1. 您可以为特定站点的所有页面创建一个父类,然后仅从该类中进行搜索。如果我在各个站点之间使用页面类型怎么办?

  2. 您可以在执行Elasticsearch之后在所有结果上调用page.get_site()-这效率低下。 https://docs.wagtail.io/en/v1.10.1/reference/pages/model_reference.html#wagtail.wagtailcore.models.Page.get_site

在幕后,get_site最终调用了这种效率低下的方法。

def get_url_parts(self):
    """
    Determine the URL for this page and return it as a tuple of
    ``(site_id, site_root_url, page_url_relative_to_site_root)``.
    Return None if the page is not routable.

    This is used internally by the ``full_url``, ``url``, ``relative_url``
    and ``get_site`` properties and methods; pages with custom URL routing
    should override this method in order to have those operations return
    the custom URLs.
    """
    for (site_id, root_path, root_url) in Site.get_site_root_paths():
        if self.url_path.startswith(root_path):
            page_path = reverse('wagtail_serve', args=(self.url_path[len(root_path):],))

            # Remove the trailing slash from the URL reverse generates if
            # WAGTAIL_APPEND_SLASH is False and we're not trying to serve
            # the root path
            if not WAGTAIL_APPEND_SLASH and page_path != '/':
                page_path = page_path.rstrip('/')

            return (site_id, root_url, page_path)
  1. 因此,我的解决方案是-在所有自定义页面模型中添加一个名为my_site_id的字段,以便在进行Elasticsearch搜索之前可以对my_site_id进行过滤。 创建和更新页面时,我将不得不覆盖页面保存以设置适当的my_site_id。 这样一来,我便可以只搜索1个网站的页面,也可以搜索3个网站中的2个。

有人知道更好的方法来控制a多站点中的跨站点搜索吗?

0 个答案:

没有答案