Lets populate some products first:
PUT /ecommerce/product/1002
{
"name": "Dell",
"price": 1200.00,
"description": "This product is awesome and I love to code in it",
"status": 1,
"quantity": 2,
"categories": [
{"name":"dell"},
{"name":"laptop"}
],
"tags":["dell", "programming", "laptop"]
}
PUT /ecommerce/product/1003
{
"name": "Dell",
"price": 1200.00,
"description": "This product is awesome and I love to design in it",
"status": 1,
"quantity": 2,
"categories": [
{"name":"dell"},
{"name":"laptop"}
],
"tags":["dell", "desigining", "laptop"]
}
In the previous post I have explained about how to create, update, read and deleting the documents in ElasticSearch. Now lets talk about Searching, Oh yeah! searching for which ElasticSearch is best about. Basically there are two ways of searching techniques which are: Query String and Query DSL. But lets talk about some basic stuff that we need to know before starting Search.
Relevancy and Scoring: To rank document for query, a score is calculated for each document that matches a query. Which means the higher the search score the document is more relevant to search query.
Relevancy and Scoring: To rank document for query, a score is calculated for each document that matches a query. Which means the higher the search score the document is more relevant to search query.
1. Query String: Its done using the search parameter in the URI through the rest request. It is specilly used for simple queries as well as for ad-hoc. e.g.
Here all fields are searched for the word "dell" by default.
GET https://localhost/ecommerce/product/_serarch?q=dell
GET https://localhost/ecommerce/product/_serarch?q=name:dell &desc:"SOME_TEXT"
GET https://localhost/ecommerce/product/_serarch?q=(name:(dell OR hp) AND status:1)
NOTE :: _search is a API used to perform a search process and q is the query parameter
and the output will be:
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.3074455,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1003",
"_score": 0.3074455,
"_source": {
"name": "Dell",
"price": 1200,
"description": "This product is awesome and I love to design in it",
"status": 1,
"quantity": 2,
"categories": [
{
"name": "dell"
},
{
"name": "laptop"
}
],
"tags": [
"dell",
"desigining",
"laptop"
]
}
}
]
}
}
2. Query DSL :: In this technique the queries are defined within the request body of the JSON. It supports more feature then the query string approach. Its often easier to read and also we can perform more advanced queries. e.g.
GET /ecommerce/product/_search
{
"query": {
"filtered": {
"filter": {
"range": {
"price": {
"from": 1000,
"to": 2000
}
}
},
"query": {
"query_string": {
"fields": ["tags"],
"query": "laptop",
"query": "hp"
}
}
}
}
}
In the above query it will first filter the products having the price in between 1000 to 2000 and then looks up for the words laptop and hp in the tags field.
GET /ecommerce/product/_search
{
"query": {
"filtered": {
"filter": {
"range": {
"price": {
"from": 1000,
"to": 2000
}
}
},
"query": {
"query_string": {
"fields": ["tags"],
"query": "laptop",
"query": "hp"
}
}
}
}
}
In the above query it will first filter the products having the price in between 1000 to 2000 and then looks up for the words laptop and hp in the tags field.
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "dell"
}
}
}
and the result will be
{
"took": 664,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.4054651,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1003",
"_score": 1.4054651,
"_source": {
"name": "Dell",
"price": 1200,
"description": "This product is awesome and I love to design in it",
"status": 1,
"quantity": 2,
"categories": [
{
"name": "dell"
},
{
"name": "laptop"
}
],
"tags": [
"dell",
"desigining",
"laptop"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1002",
"_score": 1.4054651,
"_source": {
"name": "Dell",
"price": 1200,
"description": "This product is awesome and I love to code in it",
"status": 1,
"quantity": 2,
"categories": [
{
"name": "dell"
},
{
"name": "laptop"
}
],
"tags": [
"dell",
"programming",
"laptop"
]
}
}
]
}
}
The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The angular js programming language is very popular which are most widely used.
ReplyDeleteWeb Designing Course in Chennai | web designing training in chennai