Elasticsearch

Elasticsearch Concept
Goal: Document and annotate some quick notes on learning elasticsearch.

I. CREATING YOUR OWN ANALYZER: SET UP ANALYZER SETTINGS

curl -X PUT "http://localhost:9200/wiki" -d '{
"index" : {
"number_of_shards" : 4,
"number_of_replicas" : 1,
"analysis" : {
"analyzer" : {
"content" : {
"type" : "custom",
"tokenizer" : "standard",
"filter" : ["lowercase", "stop", "kstem"],
"char_filter" : ["html_strip"]
}
}
}
}

}'

II. PUT (CREATE) MAPPING":
CREATE MAPPING (STRING -> TEXT) for INDEX

curl -X PUT "http://localhost:9200/wiki/articles/_mapping" -d '{
"articles" : {
"_all" : { "enabled" : true } ,
"properties" : {
"Title" : { "type" : "text" , "analyzer" : "content" , "include_in_all" : false, "index" : "no" },
"link" : { "type" : "text" , "include_in_all" : false, "index" : "no" },
"Author" : { "type" : "text" , "include_in_all" : false },
"timestamp" : { "type" : "date" , "format" : "dd-MM-yyyy HH:mm:ss", "include_in_all" : false },
"html" : { "type" : "text" , "analyzer" : "content", "include_in_all" : true }
}
}
}'

A. GENERATE JSON OBJECT FOR ELASTICSEARCH

python json_generator.py https://en.wikipedia.org/wiki/France > France.json
'''
# Python module script
/*import urllib2
import json
import sys

link = sys.argv[2]
htmlObj = { "link" : link,
"Author" : "anomymous",
"timestamp" : "12-12-2018 14:16:00",
"Title" : sys.argv[1]
}
response = urllib2.urlopen(link)
htmlObj['html'] = response.read()
print json.dumps(htmlObj, indent=4)
*/
'''

III. XPOST (PUSH) JSON FILE TO ELASTICSEARCH WIKI ARTICLES

curl -XPOST "http://localhost:9200/wiki/articles/" -d @France.json

IV. USING PHRASE QUERY TO SEARCH: SEARCH ELASTICSEARCH INDEX FOR CONTENT

curl -X POST "http://localhost:9200/wiki/_search?pretty" -d '{
"_source" : [
"Title"
],
"query" : {
"query_string": {
"query" : "mobile"
}
}
}'

V. USING THE HIGHLIGHTING FEATURE: SEARCH ELASTICSEARCH INDEX FOR CONTENT (HIGHLIGHTED)

# Query highlighted content
curl -X POST "http://localhost:9200/wiki/_search?pretty" -d '{
"_source" : [
"Title"
],
"query" : {
"query_string": {
"query" : "mobile"
}
}
"highlight" : {
"pre_tags" : [""],
"post_tags" : ["
"],
"fields" : {
"Content" : {}
}
}
}
}'


VI. PAGINATION

curl -X POST "http://localhost:9200/wiki/_search?pretty" -d '{
"from" : 0,
"size" : 10,
"query" : {
"simple_query_string" : {
"query" : "mobile",
"fields" : [
"_all"
]
}
},
"highlight" : {
"fields" : {
"html" : {
"pre_tags" : [
"

"
],
"post_tags" : [
"

"
],
"fragment_size" : 10,
"number_of_fragments" :3
}
}
}
}'

CHAPTER 2: BUILDING YOUR OWN E-COMMERCE SOLUTION

type
String
Integer
Long
Float
Double
Boolean
Date
geo_point
Null
Ipv4

I. CREATE (PUT) INDEX "products"

curl -X PUT "http://localhost:9200/products" -d '{
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
}
}'

II. CREATING MAPPING: Put "product" as the type for "products" index

curl -X PUT "http://localhost:9200/products/product/_mapping" -d '{
"product" : {
"properties" : {
"name" : {
"type" : "text"
},
"description" : {
"type" : "text"
},
"dateOfManufactering" : {
"type" : "date",
"format" : "YYYY-MM-dd"
},
"price" : {
"type" : "long"
},
"productType" : {
"type" : "text",
"include_in_all" : "false",
"index" : "not_analyzed"
},
"totalBuy" : {
"type" : "long",
"include_in_all": "false",
},
"imageURL" : {
"type" : "text",
"include_in_all": "false",
"index" : "no"
},
}
}
}'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s