Skip to content

Instantly share code, notes, and snippets.

@VarunVats9
Last active May 29, 2020 14:09
Show Gist options
  • Save VarunVats9/0b3e24008f0fda7969059414c969774a to your computer and use it in GitHub Desktop.
Save VarunVats9/0b3e24008f0fda7969059414c969774a to your computer and use it in GitHub Desktop.
Elasticsearch - term, range, prefix, wildcard, regex
// Term queries don't analyze the query, just search for the exact term in the inverted index >>>>>>>>>
"query": {
"term": {
"name": "Framework"
}
}
}
GET /product/_search
{
"query": {
"match": {
"name": "Framework"
}
}
}
// Understand the relevance scores >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"explain": true,
"query": {
"term": {
"name": "framework"
}
}
}
// Prefix search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"prefix": {
"name": "frame"
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Fetch all documents having non-null values >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"exists": {
"field": "status"
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Range search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"range": {
"price": {
"gte": 0.00,
"lte": 60.00
}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
}
]
}
}
// Seach based on ids >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"ids": {
"values": [ 1, 2, 3 ]
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Search by exact value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"term": {
"price": 109.00
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Mutiple terms >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"terms": {
"tags": [ "java", "framework" ]
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Search based on regex >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"regexp": {
"tags": "ja[a-zA-Z]+"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Wildcard search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search
{
"query": {
"wildcard": {
"tags": "Spr*work"
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
GET /product/_search
{
"query": {
"wildcard": {
"tags": "ja?a"
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment