2
我有一個由Postgres驅動的w site網站,並希望對所有文檔實施模糊搜索。但是,根據wagtail docs「SearchField(partial_match = True)不處理」。有誰知道我可以實現自己的部分匹配搜索的方式嗎?W Part的部分匹配搜索與Postgres
我離開這個問題故意無限期,因爲我對幾乎所有的解決方案都適用,並且具有相當的可擴展性。
我有一個由Postgres驅動的w site網站,並希望對所有文檔實施模糊搜索。但是,根據wagtail docs「SearchField(partial_match = True)不處理」。有誰知道我可以實現自己的部分匹配搜索的方式嗎?W Part的部分匹配搜索與Postgres
我離開這個問題故意無限期,因爲我對幾乎所有的解決方案都適用,並且具有相當的可擴展性。
我們目前正在重建W search搜索API,以使自動完成在後端可以使用大致相同的方式。
現在,您可以直接使用存儲搜索數據的IndexEntry
模型。不幸的是,django.contrib.postgres.search
不包含自動完成查詢的方式,所以我們現在必須自己做。這裏是如何做到這一點:
from django.contrib.postgres.search import SearchQuery
from wagtail.contrib.postgres_search.models import IndexEntry
class SearchAutocomplete(SearchQuery):
def as_sql(self, compiler, connection):
return "to_tsquery(''%s':*')", [self.value]
query = SearchAutocomplete('postg')
print(IndexEntry.objects.filter(body_search=query).rank(query))
# All results containing words starting with 「postg」
# should be displayed, sorted by relevance.
你提到的「模糊搜索」,但實際上它是別的東西。模糊搜索不是用於自動填充,而是用於拼寫建議。當然,這兩者的組合可以完成,就像谷歌一樣,在那裏自動完成可以改變查詢的拼寫。但那是另一個話題。 –