Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[SCHEMA] add new module search using postgres text search

  • Loading branch information...
commit c02beff128441251519f43e2edfd44a75295048c 1 parent 8d1553d
@leafo authored
View
13 app.moon
@@ -348,17 +348,16 @@ class MoonRocks extends lapis.Application
[search: "/search"]: =>
- @title = "search"
-
trim_filter @params
if @params.q
- query = "%" .. @params.q\lower!\gsub("%%", "") .. "%"
-
- pager = Modules\paginated [[
- where name like ?
- ]], query
+ @title = "Search '#{@params.q}'"
+ manifests = unless @params.non_root
+ { Manifests\root!.id }
+ pager = Modules\search @params.q, manifests
@results = paginated_modules @, pager
+ else
+ @title = "Search"
render: true
View
14 models/modules.moon
@@ -6,6 +6,9 @@ import concat from table
class Modules extends Model
@timestamp: true
+ @search_index: [[
+ to_tsvector('english', coalesce(display_name, name) || ' ' || coalesce(summary, '') || ' ' || coalesce(description, ''))
+ ]]
-- spec: parsed rockspec
@create: (spec, user) =>
@@ -28,6 +31,17 @@ class Modules extends Model
homepage: description.homepage
}
+ @search: (query, manifest_ids) =>
+ clause = if manifest_ids
+ ids = table.concat [tonumber id for id in *manifest_ids], ", "
+ "and exists(select 1 from manifest_modules where manifest_id in (#{ids}) and module_id = modules.id)"
+
+ @paginated [[
+ where to_tsquery('english', ?) @@ ]] .. @search_index .. [[
+ ]] .. (clause or "") .. [[
+ order by downloads desc
+ ]], query, per_page: 50
+
url_key: (name) => @name
name_for_display: =>
View
9 schema.moon
@@ -82,6 +82,14 @@ make_schema = ->
create_index "modules", "downloads"
create_index "modules", "name"
+ unless schema.entity_exists "module_search_idx"
+ import Modules from require "models"
+
+ db.query [[
+ create index module_search_idx on modules
+ using gin(]] .. Modules.search_index .. [[)
+ ]]
+
--
-- Versions
--
@@ -268,6 +276,7 @@ make_schema = ->
"PRIMARY KEY (id)"
}
+
require("lapis.exceptions.models").make_schema!
migrations.create_migrations_table!
View
6 views/search.moon
@@ -9,9 +9,13 @@ class Search extends require "widgets.base"
render_search_form: =>
form action: "", method: "get", class: "form", ->
div class: "row", ->
- label for: "search_input", "Module name"
+ label for: "search_input", "Module"
input type: "text", name: "q", id: "search_input", value: @params.q, autofocus: "autofocus"
+ div class: "row", ->
+ label for: "root_toggle", "Include non-root"
+ input type: "checkbox", name: "non_root", id: "root_toggle", checked: @params.non_root and "checked" or nil
+
div ->
input type: "submit", value: "Search"
Please sign in to comment.
Something went wrong with that request. Please try again.