-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Suggester example code and docs (#144)
* Add the suggester endpoint * Add suggestion documentation and options
- Loading branch information
1 parent
6f24b82
commit d473a3f
Showing
9 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters