1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 13:49:42 +02:00

Make search re-indexing multi-tenant aware

Makes things easier when having to reindex in multi-tenant deployments.
This commit is contained in:
Harvey Kandola 2018-10-06 17:23:55 +01:00
parent 85b3f6e8ee
commit 8eb930d281
7 changed files with 731 additions and 684 deletions

View file

@ -28,7 +28,7 @@ type Store struct {
store.AttachmentStorer store.AttachmentStorer
} }
// Add inserts the given record into the database attachement table. // Add inserts the given record into the database attachment table.
func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err error) { func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err error) {
a.OrgID = ctx.OrgID a.OrgID = ctx.OrgID
a.Created = time.Now().UTC() a.Created = time.Now().UTC()
@ -64,7 +64,7 @@ func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID stri
return return
} }
// GetAttachments returns a slice containing the attachement records (excluding their data) for document docID, ordered by filename. // GetAttachments returns a slice containing the attachment records (excluding their data) for document docID, ordered by filename.
func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) { func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(` err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid, SELECT id, c_refid AS refid,
@ -72,7 +72,7 @@ func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []atta
c_filename AS filename, c_extension AS extension, c_filename AS filename, c_extension AS extension,
c_created AS created, c_revised AS revised c_created AS created, c_revised AS revised
FROM dmz_doc_attachment FROM dmz_doc_attachment
WHERE c_orgid=? and c_docid=? WHERE c_orgid=? AND c_docid=?
ORDER BY c_filename`), ORDER BY c_filename`),
ctx.OrgID, docID) ctx.OrgID, docID)
@ -88,7 +88,7 @@ func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []atta
return return
} }
// GetAttachmentsWithData returns a slice containing the attachement records (including their data) for document docID, ordered by filename. // GetAttachmentsWithData returns a slice containing the attachment records (including their data) for document docID, ordered by filename.
func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) { func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(` err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid, SELECT id, c_refid AS refid,

View file

@ -57,7 +57,7 @@ func (s Store) Get(ctx domain.RequestContext, id string) (document doc.Document,
c_lifecycle AS lifecycle, c_versioned AS versioned, c_versionid AS versionid, c_lifecycle AS lifecycle, c_versioned AS versioned, c_versionid AS versionid,
c_versionorder AS versionorder, c_groupid AS groupid, c_created AS created, c_revised AS revised c_versionorder AS versionorder, c_groupid AS groupid, c_created AS created, c_revised AS revised
FROM dmz_doc FROM dmz_doc
WHERE c_orgid=? and c_refid=?`), WHERE c_orgid=? AND c_refid=?`),
ctx.OrgID, id) ctx.OrgID, id)
if err != nil { if err != nil {

View file

@ -204,7 +204,7 @@ func (h *Handler) Reindex(w http.ResponseWriter, r *http.Request) {
func (h *Handler) rebuildSearchIndex(ctx domain.RequestContext) { func (h *Handler) rebuildSearchIndex(ctx domain.RequestContext) {
method := "meta.rebuildSearchIndex" method := "meta.rebuildSearchIndex"
docs, err := h.Store.Meta.GetDocumentsID(ctx) docs, err := h.Store.Meta.Documents(ctx)
if err != nil { if err != nil {
h.Runtime.Log.Error(method, err) h.Runtime.Log.Error(method, err)
return return
@ -215,23 +215,23 @@ func (h *Handler) rebuildSearchIndex(ctx domain.RequestContext) {
for i := range docs { for i := range docs {
d := docs[i] d := docs[i]
doc, err := h.Store.Document.Get(ctx, d) dc, err := h.Store.Meta.Document(ctx, d)
if err != nil { if err != nil {
h.Runtime.Log.Error(method, err) h.Runtime.Log.Error(method, err)
return // continue
} }
at, err := h.Store.Attachment.GetAttachments(ctx, d) at, err := h.Store.Meta.Attachments(ctx, d)
if err != nil { if err != nil {
h.Runtime.Log.Error(method, err) h.Runtime.Log.Error(method, err)
return // continue
} }
h.Indexer.IndexDocument(ctx, doc, at) h.Indexer.IndexDocument(ctx, dc, at)
pages, err := h.Store.Meta.GetDocumentPages(ctx, d) pages, err := h.Store.Meta.Pages(ctx, d)
if err != nil { if err != nil {
h.Runtime.Log.Error(method, err) h.Runtime.Log.Error(method, err)
return // continue
} }
for j := range pages { for j := range pages {

View file

@ -13,9 +13,11 @@ package meta
import ( import (
"database/sql" "database/sql"
"github.com/documize/community/model/doc"
"github.com/documize/community/domain" "github.com/documize/community/domain"
"github.com/documize/community/domain/store" "github.com/documize/community/domain/store"
"github.com/documize/community/model/attachment"
"github.com/documize/community/model/page" "github.com/documize/community/model/page"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -26,9 +28,9 @@ type Store struct {
store.MetaStorer store.MetaStorer
} }
// GetDocumentsID returns every document ID value stored. // Documents returns every document ID value stored.
// The query runs at the instance level across all tenants. // The query runs at the instance level across all tenants.
func (s Store) GetDocumentsID(ctx domain.RequestContext) (documents []string, err error) { func (s Store) Documents(ctx domain.RequestContext) (documents []string, err error) {
err = s.Runtime.Db.Select(&documents, `SELECT c_refid FROM dmz_doc WHERE c_lifecycle=1`) err = s.Runtime.Db.Select(&documents, `SELECT c_refid FROM dmz_doc WHERE c_lifecycle=1`)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -42,8 +44,27 @@ func (s Store) GetDocumentsID(ctx domain.RequestContext) (documents []string, er
return return
} }
// GetDocumentPages returns a slice containing all published page records for a given documentID, in presentation sequence. // Document fetches the document record with the given id fromt the document table and audits that it has been got.
func (s Store) GetDocumentPages(ctx domain.RequestContext, documentID string) (p []page.Page, err error) { func (s Store) Document(ctx domain.RequestContext, id string) (document doc.Document, err error) {
err = s.Runtime.Db.Get(&document, s.Bind(`
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_userid AS userid,
c_job AS job, c_location AS location, c_name AS name, c_desc AS excerpt, c_slug AS slug,
c_tags AS tags, c_template AS template, c_protection AS protection, c_approval AS approval,
c_lifecycle AS lifecycle, c_versioned AS versioned, c_versionid AS versionid,
c_versionorder AS versionorder, c_groupid AS groupid, c_created AS created, c_revised AS revised
FROM dmz_doc
WHERE c_refid=?`),
id)
if err != nil {
err = errors.Wrap(err, "execute select document")
}
return
}
// Pages returns a slice containing all published page records for a given documentID, in presentation sequence.
func (s Store) Pages(ctx domain.RequestContext, documentID string) (p []page.Page, err error) {
err = s.Runtime.Db.Select(&p, s.Bind(` err = s.Runtime.Db.Select(&p, s.Bind(`
SELECT id, c_refid AS refid, c_orgid AS orgid, c_docid AS documentid, SELECT id, c_refid AS refid, c_orgid AS orgid, c_docid AS documentid,
c_userid AS userid, c_contenttype AS contenttype, c_userid AS userid, c_contenttype AS contenttype,
@ -65,6 +86,30 @@ func (s Store) GetDocumentPages(ctx domain.RequestContext, documentID string) (p
return return
} }
// Attachments returns a slice containing the attachment records (excluding their data) for document docID, ordered by filename.
func (s Store) Attachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid,
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
c_filename AS filename, c_extension AS extension,
c_created AS created, c_revised AS revised
FROM dmz_doc_attachment
WHERE c_docid=?
ORDER BY c_filename`),
docID)
if err == sql.ErrNoRows {
err = nil
a = []attachment.Attachment{}
}
if err != nil {
err = errors.Wrap(err, "execute select attachments")
return
}
return
}
// SearchIndexCount returns the numnber of index entries. // SearchIndexCount returns the numnber of index entries.
func (s Store) SearchIndexCount(ctx domain.RequestContext) (c int, err error) { func (s Store) SearchIndexCount(ctx domain.RequestContext) (c int, err error) {
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_search") row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_search")

View file

@ -290,7 +290,9 @@ type GroupStorer interface {
// MetaStorer provide specialist methods for global administrators. // MetaStorer provide specialist methods for global administrators.
type MetaStorer interface { type MetaStorer interface {
GetDocumentsID(ctx domain.RequestContext) (documents []string, err error) Documents(ctx domain.RequestContext) (documents []string, err error)
GetDocumentPages(ctx domain.RequestContext, documentID string) (p []page.Page, err error) Document(ctx domain.RequestContext, documentID string) (d doc.Document, err error)
Pages(ctx domain.RequestContext, documentID string) (p []page.Page, err error)
Attachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error)
SearchIndexCount(ctx domain.RequestContext) (c int, err error) SearchIndexCount(ctx domain.RequestContext) (c int, err error)
} }

View file

@ -40,7 +40,7 @@ func main() {
rt.Product.Major = "1" rt.Product.Major = "1"
rt.Product.Minor = "71" rt.Product.Minor = "71"
rt.Product.Patch = "0" rt.Product.Patch = "0"
rt.Product.Revision = 181005170824 rt.Product.Revision = 181006150624
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch) rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
rt.Product.Edition = "Community" rt.Product.Edition = "Community"
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition) rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)

File diff suppressed because one or more lines are too long