Skip to content

Commit

Permalink
Add Suggester example code and docs (#144)
Browse files Browse the repository at this point in the history
* Add the suggester endpoint

* Add suggestion documentation and options
  • Loading branch information
Firesphere authored and marczhermo committed Nov 28, 2019
1 parent 6f24b82 commit d473a3f
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Solr/5/extras/solrconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,19 @@
</arr>
</requestHandler>


<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
<lst name="defaults">
<str name="suggest.dictionary">Suggester</str>
<str name="suggest">true</str>
<str name="suggest.count">10</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>


<!-- A request handler that returns indented JSON by default -->
<requestHandler name="/query" class="solr.SearchHandler">
<lst name="defaults">
Expand Down Expand Up @@ -1672,6 +1685,18 @@
</highlighting>
</searchComponent>


<searchComponent name="suggest" class="solr.SuggestComponent">
<lst name="suggester">
<str name="name">Suggester</str>
<str name="field">_text</str>
<str name="suggestAnalyzerFieldType">string</str>
<str name="buildOnStartup">true</str>
<str name="buildOnCommit">true</str>
</lst>
</searchComponent>


<!-- Update Processors
Chains of Update Processor Factories for dealing with Update
Expand Down
6 changes: 6 additions & 0 deletions client/proxy/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Order deny,allow
Deny from all

<Files "autosuggest.php">
Allow from all
</Files>
11 changes: 11 additions & 0 deletions client/proxy/autosuggest.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<?php

use GuzzleHttp\Client;

if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php')) {
require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
}

$request = filter_input(INPUT_GET, 'query', FILTER_SANITIZE_STRING);

$client = new Client();
$suggestions = $client->request('GET', 'http://localhost:8983/solr/My-Solr-Core/suggest?q=' . $request);
header('Content-Type: application/json');
echo($suggestions->getBody());
90 changes: 90 additions & 0 deletions docs/08-Suggestions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Solr Suggest

If you are using Solr5 or above, you can use the Solr Suggest option. This is enabled by default.

To set this up, have a look at [autosuggest proxy](https://github.com/Firesphere/silverstripe-solr-search/blob/master/client/proxy/autosuggest.php)

Copy this file to a convenient location, e.g. your `docroot/public` folder and edit the contents of your copy, to match your Solr core and location of the Solr instance.

Note that the proxy can not read from the Silverstripe config, thus it needs to be edited manually.

Once that's in place, you can use javascript to get Solr's suggestions, by querying the autosuggest.php file directly.

You need to query the file directly, to prevent a full execution of the whole Silverstripe stack, which is too slow for proper auto suggesting.
Also, make sure the file is accessible.

The output of the file is a JSON object, looking something like this:
```json
{

"responseHeader": {
"status": 0,
"QTime": 0
},
"suggest": {
"Suggester": {
"home": {
"numFound": 10,
"suggestions": [
{
"term": "home",
"weight": 376,
"payload": ""
},
{
"term": "homeajaxaction",
"weight": 1,
"payload": ""
},
{
"term": "homebartonvillevpsbartonvilletccommysitecodememberextensionphp",
"weight": 1,
"payload": ""
},
{
"term": "homeblauwboomdomainsmyserverpublichtmlcompframeworksrci",
"weight": 1,
"payload": ""
},
{
"term": "homebrew",
"weight": 26,
"payload": ""
},
{
"term": "homebrewcurlother",
"weight": 1,
"payload": ""
},
{
"term": "homebrewhttpsbrewsh",
"weight": 1,
"payload": ""
},
{
"term": "homebrewing",
"weight": 1,
"payload": ""
},
{
"term": "homebridgewillstaginpublichtmlsilvershopcodecartordertotalcalculatorphp",
"weight": 1,
"payload": ""
},
{
"term": "homecategorycategory",
"weight": 1,
"payload": ""
}
]
}
}
}
}
```

Which you can then use in your javascript to populate a dropdown.

## Security note

As the query is passed straight in to Solr, there is no option of database SQL injection.
File renamed without changes.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
05. [CMS Usage](05-CMS-Usage.md)
06. [Fulltext Search compatibility](06-Fulltext-Search-Compatibility.md)
07. [Debugging](07-Debugging.md)
08. [About](08-About.md)
08. [Suggestions](08-Suggestions.md)
10. [About](10-About.md)
11. [FAQ](11-FAQ.md)
7 changes: 7 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ en:
one: 'A Dirty Class'
other: '{count} Dirty Classs'
SINGULARNAME: 'Dirty Class'
Firesphere\SolrSearch\Models\SearchSynonym:
PLURALNAME: 'Search synonyms'
PLURALS:
one: 'A Search synonym'
other: '{count} Search synonyms'
SINGULARNAME: 'Search synonym'
SYNONYM: 'Create synonyms for a given keyword, add as many synonyms comma separated.'
Firesphere\SolrSearch\Models\SolrLog:
PERMISSION_DELETE_DESCRIPTION: 'Delete Solr logs'
PERMISSION_DELETE_HELP: 'Permission required to delete existing Solr logs.'
Expand Down
2 changes: 1 addition & 1 deletion src/Indexes/BaseIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function getSynonyms($store = null, $defaults = true)
$synonyms = Synonyms::getSynonymsAsString($defaults);
$syn = SearchSynonym::get();
foreach ($syn as $synonym) {
$synonyms .= "\n" . $synonym->Keyword . ',' . $synonym->Synonym;
$synonyms .= $synonym->Keyword . ',' . $synonym->Synonym . PHP_EOL;
}

// Upload synonyms
Expand Down
8 changes: 8 additions & 0 deletions src/Models/DirtyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class DirtyClass extends DataObject
* @var string Table name
*/
private static $table_name = 'DirtyClass';
/**
* @var string Singular name
*/
private static $singular_name = 'Dirty class';
/**
* @var string Plural name
*/
private static $plural_name = 'Dirty classes';
/**
* @var array Database fields
*/
Expand Down

0 comments on commit d473a3f

Please sign in to comment.