Skip to content

Commit

Permalink
A few small fixes + better documentation + sample solr config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Guinn committed May 23, 2014
1 parent b35e6cb commit 81cd688
Show file tree
Hide file tree
Showing 13 changed files with 5,986 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ INSTALLATION:
-------------
1. Install via composer
2. Make adjustments to configuration via yml. May want to change adapter
class, searchable classes, and facets.
class, searchable classes, and facets. **See shop_search/docs/en/Adapters.md**.
3. If using VirtualFieldIndex class for faceting with mysql, enable a
cron job for `dev/tasks/BuildVFI`.

Expand Down
5 changes: 5 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Name: shopsearch
ContentController:
extensions:
- ShopSearchControllerExtension

LeftAndMain:
extra_requirements_css:
- shop_search/css/ShopSearchCMS.css

Product:
extensions:
- ProductCategoryHelperMethods
126 changes: 126 additions & 0 deletions code/helpers/ProductCategoryHelperMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Adds some methods to the Product class to list out all categories
* a product is in. This is generally useful, but also really helps
* with building search indexes.
*
* @author Mark Guinn <[email protected]>
* @date 05.23.2014
* @package shop_search
* @subpackage helpers
*/
class ProductCategoryHelperMethods extends DataExtension
{
/**
* Includes Parent() and ProductCategories() and all the parent categories of each.
* @param bool $recursive [optional] - include the parents of the categories as well? - default: true
* @return array - array of productCategory records
*/
protected function buildParentCategoriesArray($recursive=true) {
$out = array();

// add the main parent
$parent = $this->owner->Parent();
if (!$parent || !$parent->exists()) return $out;
if ($parent->ClassName == 'ProductCategory') $out[$parent->ID] = $parent;

if ($recursive) {
foreach ($parent->getAncestors() as $rec) {
if ($rec->ClassName == 'ProductCategory') $out[$rec->ID] = $rec;
}
}

// add any secondary categories
foreach ($this->owner->ProductCategories() as $cat) {
$out[$cat->ID] = $cat;
if ($recursive) {
foreach ($cat->getAncestors() as $rec) {
if ($rec->ClassName == 'ProductCategory') $out[$rec->ID] = $rec;
}
}
}

return $out;
}


/**
* Includes Parent() and ProductCategories() and all the parent categories of each.
* @return ArrayList - array of productCategory records
*/
public function getAllCategories() {
return new ArrayList( $this->buildParentCategoriesArray(false) );
}


/**
* Includes Parent() and ProductCategories() and all the parent categories of each.
* @return ArrayList - array of productCategory records
*/
public function getAllCategoriesRecursive() {
return new ArrayList( $this->buildParentCategoriesArray(true) );
}


/**
* Includes Parent() and ProductCategories()
* @return array - array of ID's
*/
public function getAllCategoryIDs() {
return array_keys( $this->buildParentCategoriesArray(false) );
}


/**
* Includes Parent() and ProductCategories() and all the parent categories of each.
* @return array - array of ID's
*/
public function getAllCategoryIDsRecursive() {
return array_keys( $this->buildParentCategoriesArray(true) );
}


/**
* Returns an array of the titles of all the categories.
* @return array - (strings)
*/
public function getAllCategoryTitles() {
$cats = $this->buildParentCategoriesArray(false);
$out = array();
foreach ($cats as $cat) $out[] = $cat->Title;
return $out;
}


/**
* Returns an array of the titles of all the categories, including grandparents, etc
* @return array - (strings)
*/
public function getAllCategoryTitlesRecursive() {
$cats = $this->buildParentCategoriesArray(true);
$out = array();
foreach ($cats as $cat) $out[] = $cat->Title;
return $out;
}


/**
* Returns a string of the titles of all the categories.
* @param string $sep
* @return string
*/
public function getJoinedCategoryTitles($sep = ', ') {
return implode($sep, $this->getAllCategoryTitles());
}


/**
* Returns a string of the titles of all the categories.
* @param string $sep
* @return string
*/
public function getJoinedCategoryTitlesRecursive($sep = ', ') {
return implode($sep, $this->getAllCategoryTitlesRecursive());
}

}
23 changes: 20 additions & 3 deletions docs/en/Adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,30 @@ NOTE: Paging is not yet implemented

ShopSearchSolr
--------------
Follow the instructions on the fulltextsearch module to get solr running.
This adapter is the quickest and most feature-complete. Fulltext and filter
fields are set using yml configuration options on the ShopSearch class.
This is because there is additional config that may need to happen. An
example would be the following:
This is because there is additional config that may need to happen.

First, add something like the following to _config.php:

```
// Configure Solr
Solr::configure_server(array(
'host' => 'localhost',
'extraspath' => Director::baseFolder() . '/shop_search/solr/',
'indexstore' => array(
'mode' => 'file',
'path' => BASE_PATH . '/.solr'
)
));
```

An example `mysite/_config/search.yml` would be the following:

```
---
Name: appsearch
---
ShopSearch:
adapter_class: ShopSearchSolr
buyables_are_searchable: 0
Expand Down
7 changes: 5 additions & 2 deletions javascript/search.suggest.solr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
if (typeof window.ShopSearch == 'undefined') window.ShopSearch = {};

/**
* TODO: internationalize this
* @param priceIn
* @returns string
*/
Expand Down Expand Up @@ -149,8 +150,10 @@
$('<span>').addClass('title').html(item.title).appendTo(a);

if (item.desc) $('<span>').addClass('desc').html(item.desc).appendTo(a);
var price = $('<span>').addClass('price').html(item.price).appendTo(a);
if (item.original_price) price.prepend('<del>'+item.original_price+'</del> ');
if (item.price && item.price != '$0.00') {
var price = $('<span>').addClass('price').html(item.price).appendTo(a);
if (item.original_price) price.prepend('<del>'+item.original_price+'</del> ');
}

a.appendTo(li);
} else {
Expand Down
6 changes: 6 additions & 0 deletions solr/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The files in this folder are examples of how you might configure solr.
They are largely copied verbatim from fulltextsearch-localsolr with
some changes added for search suggestions and search-as-you-type.

If you want to customise them just copy them to another folder or
ditch them entirely and change your extrasPath under Solr::configure_server.
Empty file added solr/_manifest_exclude
Empty file.
36 changes: 36 additions & 0 deletions solr/elevate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- If this file is found in the config directory, it will only be
loaded once at startup. If it is found in Solr's data
directory, it will be re-loaded every commit.
-->

<elevate>
<query text="foo bar">
<doc id="1" />
<doc id="2" />
<doc id="3" />
</query>

<query text="ipod">
<doc id="MA147LL/A" /> <!-- put the actual ipod at the top -->
<doc id="IW-02" exclude="true" /> <!-- exclude this cable -->
</query>

</elevate>
Loading

0 comments on commit 81cd688

Please sign in to comment.