我正在尝试使用Postgrex提供的to扩展。但是我如何在我的Phoeinx项目中使用这个呢?这是我在dev.exs中的设置
config :project, Project.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "project_dev",
hostname: "localhost",
pool_size: 10,
extensions: [{Postgrex.Extensions.TSVector}]这是模式
schema "branches" do
field :address, :string
field :tags, {:array, :string}
field :document, Postgrex.Extensions.TSVector
belongs_to :bank, Bank
timestamps()
end这难道不应该给我一种新型的TSVector吗?我收到以下错误
** (ArgumentError) invalid or unknown type Postgrex.Extensions.TSVector for field :document更新
我尝试将架构设置为
field :document, {:array, Postgrex.Lexeme}在从数据库中检索值时,我得到以下错误** (UndefinedFunctionError) function Postgrex.Lexeme.type/0 is undefined or private
发布于 2018-05-07 08:08:31
我也遇到了同样的问题。默认情况下,在tsvector请求:https://github.com/elixir-ecto/postgrex/pull/284之后,Postgrex包含/加载了https://github.com/elixir-ecto/postgrex/pull/284扩展,因此没有必要向Project.Repo配置中添加任何:extensions选项。
我在这样的迁移中创建了该列:
def change do
alter table(:diagnoses) do
add :search_tsvector, :tsvector
end
execute "UPDATE diagnoses SET search_tsvector = to_tsvector('english', concat_ws(' ', description, icd10_code))"
create index(:diagnoses, [:search_tsvector], using: :gin)
end之后,您就不能简单地将:tsvector添加到模块模式中,因为不存在:tsvector Ecto类型(由于扩展而只存在Postgrex类型)。
要在我的模式中使用这个新列,我需要添加一个定制的Ecto类型,该类型只有默认的样板定义:
defmodule MyApp.Ecto.Types.TSVectorType do
@behaviour Ecto.Type
def type, do: :tsvector
def cast(tsvector), do: {:ok, tsvector}
def load(tsvector), do: {:ok, tsvector}
def dump(tsvector), do: {:ok, tsvector}
end之后,您可以将此类型包含在模块schema中。
schema "diagnoses" do
field :icd10_code, :string
field :description, :string
field :search_tsvector, MyApp.Ecto.Types.TSVectorType
timestamps_with_deleted_at()
end我实现了像这样搜索这个tsvector字段:
defp filter_by(query, :search_string, %{search_string: search_string} = args) do
tsquery_string = StringHelpers.to_tsquery_string(search_string)
from d in query,
where: fragment("? @@ to_tsquery('english', ?)", d.search_tsvector, ^tsquery_string)
endhttps://stackoverflow.com/questions/46464051
复制相似问题