Getting started

advertisement
THEMATCHBOX
Getting started
!
Get started with ElasticSearch and find out how to install a theMatchBox plugin.
!
1. Prerequisites
2
2. Installing ElasticSearch
2
3. Installing a theMatchBox plugin
2
4. Running ElasticSearch
3
5. Adding an index
3
6. Adding a mapping
3
7. Adding data
4
8. Searching data
4
THEMATCHBOX
1. PREREQUISITES
!
• You need the most recent version of Java 7 or Java 8. You can check your version in a command line with:
java -version
• JAVA_HOME needs to be set. Check in a command line with:
echo $JAVA_HOME on Mac/Linux
echo %JAVA_HOME% on Windows
• You need a tool to test a RESTful web service, e.g.:
• A curl command line tool (for Windows under Cygwin)
• Postman plugin for Chrome
• RESTClient add-on for Firefox
• Many others
!
2. INSTALLING ELASTICSEARCH
!
• Download the latest version of ElasticSearch on the ElasticSearch website (http://www.elasticsearch.org/)
• Unzip the ElasticSearch distribution where you want it installed
!
3. INSTALLING A THEMATCHBOX PLUGIN
!
Adding a theMatchBox text analyzer is easy. You can install the plugin in a command line using the plugin
script from ElasticSearch:
./bin/plugin -i theMatchBox -u file:thematchboxplugin.zip
The plugin tool actually unzips it in the plugin folder in the ElasticSearch root directory.
You can check which plugins are being used with:
curl -XGET http://localhost:9200/_nodes
!
THEMATCHBOX
4. RUNNING ELASTICSEARCH
!
• Go to the ElasticSearch bin-folder
• Start the script to run it:
bin/elasticsearch on Mac/Linux
bin/elasticsearch.bat on Windows
• Check if its running in a web browser by surfing to http://localhost:9200/
or in a command line with curl
!
-XGET http://localhost:9200/
5. ADDING AN INDEX
!
Before adding data, you need to create an index. To create an index, use the http PUT-command and add the
name of the index (only lowercase allowed):
curl -XPUT 'http://localhost:9200/my_demo_index'
!
6. ADDING A MAPPING
!
ElasticSearch uses JSON for mapping, indexing and searching. JSON is a lightweight data-interchange format
thats easy to parse and generate. It is simple and easy to read. Learn more about JSON on http://json.org/.
To add a mapping use the name of the index, the type of the document and append the document content as
a JSON object:
curl -XPUT 'http://localhost:9200/my_demo_index/Vacancy/_mapping?pretty' -d '{
"Vacancy": {
"properties": {
"title": {
"type": "string",
"index": "analyzed",
"analyzer": "matchbox_analyzer"
},
"description": {
"type": "string",
"index": "analyzed",
"analyzer": "matchbox_analyzer"
}
}
}
}’
!
THEMATCHBOX
7. ADDING DATA
!
To add a document use the name of the index, the type of the document and a unique document ID (typically
the unique id from the database). Append the document content as a JSON object:
curl -XPUT 'http://localhost:9200/my_demo_index2/Vacancy/1?pretty' -d '{
"title" : "KAP(P)(ST)ER (M/V)",
"description" : "Wij zijn op zoek naar een gemotiveerde kapper/kapster die zal instaan voor
alle mogelijke werkzaamheden in een kapsalon zoals wassen, knippen, verven, ... dit zowel voor
dames, heren als kinderen."
}'
!
curl -XPUT 'http://localhost:9200/my_demo_index2/Vacancy/2?pretty' -d '{
"title" : "5 Kapper / Kapster (M/V)",
"description" : "je kan zelfstandig alle voorkomende taken in een kapsalon uitoefenen:
wassen, knippen, permanent, kleuren,.. en dit zowel voor dames, heren als kinderen"
}'
!
!
curl -XPUT 'http://localhost:9200/my_demo_index2/Vacancy/3?pretty' -d '{
"title" : "5 Kapper / Kapster (M/V)",
"description" : "In ons salon zorgt u voor de verzorging van het haar van onze klanten. U
staat zelfstandig in voor het wassen, kleuren, knippen, brushen, enz."
}'
8. SEARCHING DATA
!
Searching the index with a URI-query:
!!
curl -XGET 'http://localhost:9200/my_demo_index2/Vacancy/_search?pretty&q=title:kapster'
Searching the index with a request body query:
curl -XPOST 'http://localhost:9200/my_demo_index2/Vacancy/_search?pretty' -d '{
"query": {
"match": {
"title" : "kpapsters"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}’
!!
Above we added a spelling mistake to the query, with theMatchBox Language Analyzer-plugin installed, you
get the following response (note the highlights):
THEMATCHBOX
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.19178301,
"hits" : [ {
"_index" : "my_demo_index2",
"_type" : "Vacancy",
"_id" : "1",
"_score" : 0.19178301,
"_source":{
"title" : "KAP(P)(ST)ER (M/V)",
"description" : "Wij zijn op zoek naar een gemotiveerde kapper/kapster die zal instaan voor
alle mogelijke werkzaamheden in een kapsalon zoals wassen, knippen, verven, ... dit zowel voor
dames, heren als kinderen."
}
,
"highlight" : {
"title" : [ "<em>KAP(P)(ST)ER</em> (M/V)" ]
}
}, {
"_index" : "my_demo_index2",
"_type" : "Vacancy",
"_id" : "2",
"_score" : 0.18985549,
"_source":{
"title" : "5 Kapper / Kapster (M/V)",
"description" : "je kan zelfstandig alle voorkomende taken in een kapsalon uitoefenen:
wassen, knippen, permanent, kleuren,.. en dit zowel voor dames, heren als kinderen"
},
"highlight" : {
"title" : [ "5 <em>Kapper</em> / <em>Kapster</em> (M/V)" ]
}
}, {
"_index" : "my_demo_index2",
"_type" : "Vacancy",
"_id" : "3",
"_score" : 0.18985549,
"_source":{
"title" : "5 Kapper / Kapster (M/V)",
"description" : "In ons salon zorgt u voor de verzorging van het haar van onze klanten. U
staat zelfstandig in voor het wassen, kleuren, knippen, brushen, enz."
},
"highlight" : {
"title" : [ "5 <em>Kapper</em> / <em>Kapster</em> (M/V)" ]
}
} ]
}
}
Download