# Design документы для CouchDB module.exports = # Design документ для блог постов blog_posts: version: "1.1" views: # Все опубликованные блог посты published: map: ((doc) -> if doc.type is 'blog_post' and doc.status is 'published' emit(doc.created_at, doc)).toString() # Блог посты по тегам by_tag: map: (doc) -> if doc.type is 'blog_post' and doc.status is 'published' and doc.tags for tag in doc.tags emit([tag, doc.created_at], doc) # Блог посты по автору by_author: map: (doc) -> if doc.type is 'blog_post' and doc.status is 'published' emit([doc.author, doc.created_at], doc) # Поиск по заголовку и содержанию search: map: (doc) -> if doc.type is 'blog_post' and doc.status is 'published' # Индексируем заголовок и содержание для поиска text = (doc.title + " " + doc.content + " " + doc.excerpt).toLowerCase() words = text.split(/\W+/).filter (word) -> word.length > 2 for word in words emit(word, { _id: doc._id title: doc.title excerpt: doc.excerpt created_at: doc.created_at }) # Design документ для мероприятий events: version: "1.0" views: # Все мероприятия по дате by_date: map: (doc) -> if doc.type is 'event' emit(doc.event_date, doc) # Предстоящие мероприятия upcoming: map: (doc) -> if doc.type is 'event' and doc.status is 'upcoming' emit(doc.event_date, doc) # Активные мероприятия ongoing: map: (doc) -> if doc.type is 'event' and doc.status is 'ongoing' emit(doc.event_date, doc) # Мероприятия по тегам by_tag: map: (doc) -> if doc.type is 'event' and doc.tags for tag in doc.tags emit([tag, doc.event_date], doc) # Мероприятия по местоположению by_location: map: (doc) -> if doc.type is 'event' emit([doc.location, doc.event_date], doc) # Design документ для слайдов slides: version: "1.0" views: # Активные слайды по порядку active_ordered: map: (doc) -> if doc.type is 'slide' and doc.active is true emit(doc.order, doc) # Design документ для товаров products: version: "1.0" views: # Все доступные товары available: map: (doc) -> if doc.type is 'product' and doc.status is 'available' emit(doc.created_at, doc) # Товары по категориям by_category: map: (doc) -> if doc.type is 'product' and doc.status is 'available' emit([doc.category, doc.created_at], doc) # Товары по тегам by_tag: map: (doc) -> if doc.type is 'product' and doc.tags for tag in doc.tags emit([tag, doc.created_at], doc) # Товары по цене by_price: map: (doc) -> if doc.type is 'product' and doc.status is 'available' emit(doc.price, doc) # Design документ для категорий и тем categories_themes: version: "1.0" views: # Все категории по порядку categories_ordered: map: (doc) -> if doc.type is 'category' emit(doc.order, doc) # Категории по родителю (для иерархии) categories_by_parent: map: (doc) -> if doc.type is 'category' parent = doc.parent or 'root' emit([parent, doc.order], doc) # Все темы themes: map: (doc) -> if doc.type is 'theme' emit(doc.name, doc) # Design документ для глобального поиска global_search: version: "1.0" views: # Глобальный поиск по всем типам документов all_content: map: (doc) -> # Индексируем различные типы документов для поиска searchFields = {} switch doc.type when 'blog_post' searchFields = title: doc.title content: doc.content excerpt: doc.excerpt author: doc.author tags: doc.tags type: 'blog_post' when 'event' searchFields = title: doc.title content: doc.content location: doc.location tags: doc.tags type: 'event' when 'product' searchFields = title: doc.title content: doc.content excerpt: doc.excerpt category: doc.category tags: doc.tags type: 'product' when 'category', 'theme' searchFields = name: doc.name description: doc.description type: doc.type # Создаем поисковый индекс if searchFields.title text = ( searchFields.title + " " + (searchFields.content or "") + " " + (searchFields.excerpt or "") + " " + (searchFields.author or "") + " " + (searchFields.location or "") + " " + (searchFields.description or "") ).toLowerCase() # Добавляем теги if searchFields.tags text += " " + searchFields.tags.join(" ") words = text.split(/\W+/).filter (word) -> word.length > 2 for word in words emit(word, { _id: doc._id type: searchFields.type title: searchFields.title excerpt: searchFields.excerpt created_at: doc.created_at }) # Design документ для статистики statistics: version: "1.0" views: # Статистика по типам документов by_type: map: (doc) -> emit(doc.type, 1) reduce: (keys, values) -> sum values # Статистика просмотров блог постов blog_views: map: (doc) -> if doc.type is 'blog_post' emit(doc._id, doc.views or 0) reduce: (keys, values) -> sum values # Статистика мероприятий по статусу events_by_status: map: (doc) -> if doc.type is 'event' emit(doc.status, 1) reduce: (keys, values) -> sum values