SlideShare a Scribd company logo
Querying Elasticsearch
Binary Studio Academy PRO 2016
binary-studio.com
Search Types
STRUCTURE
(FIELD)
SEARCH
FULL-TEXT
SEARCH
Search APIs
● Lite (query string) search
● Full-body search
Lite search
http
localhost:9200/github/repository/_search?q=language:Javascript%20+forks_count:
%3E20000&sort=forks_count:desc&size=3
Lite search
Expects all parameters to be passed via query string and encoded properly e.g:
http localhost:9200/github/repository/_search?q=name:angular.js
Based on _search API:
http localhost:9200/_search http localhost:9200/user,repository
http localhost:9200/{index}/{type}/_search?q=field:value...
http localhost:9200/github/repository/_search?size=2&from=50
Lite search
Supports pagination:
Supports obligatory conditions (+  -):
http localhost:9200/github/repository/_search?q=+language:(php%20css)
Supports sorting
http
localhost:9200/github/repository/_search?q=language:Java&sort=watchers_count:d
esc
Lite search
PROS
Powerful
Convenient for development and ad-
hoc queries
End-users can run queries directly
from their web-browser
CONS
Queries should be carefuly encoded
Opened API can cause potentially
slow queries or even kill your
cluster
Not so efficient for complex queries
FULL-BODY
SEARCH
data
FULL-BODY SEARCH
● Utilizes the same _search API
● Transfers parameters in request body e.g
curl localhost:9200/github/repository/_search -d '{"size": 2, "from": 10}'
● According to RFC 7231 there is no strict definition what to do when server
received GET query with body parameters (depends on server
implementation). So both GET and POST methods are allowed.
● Instead of encoded urls there is convenient search query domain-specific
language (DSL)
SEARCH QUERY
DSL
DSL
SEARCH QUERY CLAUSES
● Leaf clauses - compare field to a query string
(match, term, range)
● Compound clauses - combine other query clauses
(bool, dis_max)
SEARCH QUERY FORMAT
SEARCH QUERY DSL EXAMPLE
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"match": {
"language": "Javascript"
}
}
}'
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"bool": {
"must": {"match": {"language": "Javascript"}},
"should": {"match": {"description": "library"}}
}
}
}'
SEARCH QUERY MATCHERS
match
multi_match common_terms query_string
simple_query_string
FULL TEXT QUERIES
MATCHERS
MATCH & MULTI_MATCH
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"match": {
"language": "Javascript"
}
}
}'
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"multi_match": {
"query": "javascript",
"fields": ["language", "description"]
}
}
}'
QUERY STRING QUERY
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"query_string": {
"query": "language:(C OR PHP) AND watchers_count:[15000 TO *]"
}
}
}'
Supports compact Lucene query string syntax
SIMPLE QUERY STRING QUERY
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"simple_query_string": {
"fields": ["description"],
"query": "(framework^2 realtime) + -(web port client)"
}
}
}'
Have simplified query syntax
COMMON TERMS QUERY
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"common": {
"description": {
"query": "for is and web",
"cutoff_frequency": 0.001
}
}
}
}'
Divides query terms into two groups:
● More important - low frequency
● Less important - high frequency (applied first)
SEARCH QUERY FILTERS
● term
● terms
● range
● exists
● missing
● bool
● prefix
● wildcard
● regex
● fuzzy
TERM AND RANGE FILTERS
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"term": {
"language": "C++"
}
}
}'
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"range": {
"watchers_count": {
"gte": 5000,
"lte": 15000
}
}
}
}'
EXISTS AND MISSING FILTERS
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must_not": {
"exists": {
"field": "language"
}
}
}
}
}
}
}'
BOOL FILTER
● must
○ Clauses must match, like and
● must_not
○ Clauses must not match, like not
● should
○ At least one of clauses must match, like or .
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": {
"term": {
"language": "JavaScript"
}
},
"should": {
"range": {
"forks_count": {
"gt": 10000
}
}
}
}
}
}
}
COMBINING FILTERS AND MATCHERS
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"filtered": {
"query": {
"match": {
"has_issues": true
}
},
"filter": {
"term": {
"language": "Objective-C"
}
}
}
}
}'
SORTING
SORTINGcurl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"filtered": {
"query": {
"match": {
"has_issues": true
}
},
"filter": {
"term": {
"language": "Objective-C"
}
}
}
},
"sort": {
"forks_count": {
"order": "desc"
}
}
}'
RELEVANCE
RELEVANCE
● How well a retrieved document or set of documents meets the information
need (criteria) of the user
● Positive FP number stored under _score property
● Calculated by term frequency/inverce document frequency (TF/IDF) algorithm:
○ Term Frequency (tf): more often - more
relevant (field)
○ Inverted Document Frequency(idf) more often - less relevant (index)
○ Field-length norm (fieldNorm) shorter - more relevant (field)
RELEVANCE EXPLANATION
curl localhost:9200/github/repository/_search?pretty -d '{
"query": {
"term": {
"language": "C++"
}
},
"size": 1,
"explain": true
}'
TO BE CONTINUED...

More Related Content

PPTX
Webinar: Building Your First App in Node.js
PDF
maxbox starter72 multilanguage coding
PPT
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
PPTX
Getting Started with MongoDB and NodeJS
PDF
Static Typing in Vault
PDF
Into the ZF2 Service Manager
PDF
今時なウェブ開発をSmalltalkでやってみる?
PDF
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
Webinar: Building Your First App in Node.js
maxbox starter72 multilanguage coding
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
Getting Started with MongoDB and NodeJS
Static Typing in Vault
Into the ZF2 Service Manager
今時なウェブ開発をSmalltalkでやってみる?
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...

What's hot (20)

PDF
RestMQ - HTTP/Redis based Message Queue
PPTX
Visualizing ORACLE performance data with R @ #C16LV
PDF
Lua tech talk
PDF
Bootstrapping multidc observability stack
PDF
Information security programming in ruby
ODP
Integrating icinga2 and the HashiCorp suite
PDF
Advanced Relevancy Ranking
PDF
Everything as Code with Terraform
PDF
Practical Testing of Ruby Core
PDF
Swift Sequences & Collections
PDF
Logstash-Elasticsearch-Kibana
ODP
Using Logstash, elasticsearch & kibana
PDF
PPTX
HTTP Middlewares in PHP by Eugene Dounar
PPTX
Using Cerberus and PySpark to validate semi-structured datasets
PDF
Kibana: Real-World Examples
PDF
Roll Your Own API Management Platform with nginx and Lua
PDF
Little Big Ruby
PDF
Tools for Solving Performance Issues
PDF
Introduction to reactive programming & ReactiveCocoa
RestMQ - HTTP/Redis based Message Queue
Visualizing ORACLE performance data with R @ #C16LV
Lua tech talk
Bootstrapping multidc observability stack
Information security programming in ruby
Integrating icinga2 and the HashiCorp suite
Advanced Relevancy Ranking
Everything as Code with Terraform
Practical Testing of Ruby Core
Swift Sequences & Collections
Logstash-Elasticsearch-Kibana
Using Logstash, elasticsearch & kibana
HTTP Middlewares in PHP by Eugene Dounar
Using Cerberus and PySpark to validate semi-structured datasets
Kibana: Real-World Examples
Roll Your Own API Management Platform with nginx and Lua
Little Big Ruby
Tools for Solving Performance Issues
Introduction to reactive programming & ReactiveCocoa
Ad

Similar to Academy PRO: Querying Elasticsearch (20)

PPTX
ElasticSearch AJUG 2013
KEY
Elasticsearch & "PeopleSearch"
PDF
Elasticsearch in 15 Minutes
PPTX
曾勇 Elastic search-intro
PPTX
Elastic search intro-@lamper
PPTX
Elasticsearch
PDF
Workshop: Learning Elasticsearch
ODP
Elastic Search
PDF
elasticsearch basics workshop
PDF
Indices APIs - Elasticsearch Reference
PPTX
Academy PRO: Elasticsearch. Data management
PPTX
Introduction to Elasticsearch Searching
PDF
Hopper Elasticsearch Hackathon
PPTX
Elastic search Walkthrough
PPTX
Introduction to Elasticsearch with basics of Lucene
PDF
Tutorial: Building a GraphQL API in PHP
PDF
Java Search Engine Framework
PPT
How ElasticSearch lives in my DevOps life
PDF
Galene - LinkedIn's Search Architecture: Presented by Diego Buthay & Sriram S...
PDF
Elasticsearch Quick Introduction
ElasticSearch AJUG 2013
Elasticsearch & "PeopleSearch"
Elasticsearch in 15 Minutes
曾勇 Elastic search-intro
Elastic search intro-@lamper
Elasticsearch
Workshop: Learning Elasticsearch
Elastic Search
elasticsearch basics workshop
Indices APIs - Elasticsearch Reference
Academy PRO: Elasticsearch. Data management
Introduction to Elasticsearch Searching
Hopper Elasticsearch Hackathon
Elastic search Walkthrough
Introduction to Elasticsearch with basics of Lucene
Tutorial: Building a GraphQL API in PHP
Java Search Engine Framework
How ElasticSearch lives in my DevOps life
Galene - LinkedIn's Search Architecture: Presented by Diego Buthay & Sriram S...
Elasticsearch Quick Introduction
Ad

More from Binary Studio (20)

PPTX
Academy PRO: D3, part 3
PPTX
Academy PRO: D3, part 1
PPTX
Academy PRO: Cryptography 3
PPTX
Academy PRO: Cryptography 1
PPTX
Academy PRO: Advanced React Ecosystem. MobX
PPTX
Academy PRO: Docker. Part 4
PPTX
Academy PRO: Docker. Part 2
PPTX
Academy PRO: Docker. Part 1
PPTX
Binary Studio Academy 2017: JS team project - Orderly
PPTX
Binary Studio Academy 2017: .NET team project - Unicorn
PPTX
Academy PRO: React native - miscellaneous
PPTX
Academy PRO: React native - publish
PPTX
Academy PRO: React native - navigation
PPTX
Academy PRO: React native - building first scenes
PPTX
Academy PRO: React Native - introduction
PPTX
Academy PRO: Push notifications. Denis Beketsky
PPTX
Academy PRO: Docker. Lecture 4
PPTX
Academy PRO: Docker. Lecture 3
PPTX
Academy PRO: Docker. Lecture 2
PPTX
Academy PRO: Docker. Lecture 1
Academy PRO: D3, part 3
Academy PRO: D3, part 1
Academy PRO: Cryptography 3
Academy PRO: Cryptography 1
Academy PRO: Advanced React Ecosystem. MobX
Academy PRO: Docker. Part 4
Academy PRO: Docker. Part 2
Academy PRO: Docker. Part 1
Binary Studio Academy 2017: JS team project - Orderly
Binary Studio Academy 2017: .NET team project - Unicorn
Academy PRO: React native - miscellaneous
Academy PRO: React native - publish
Academy PRO: React native - navigation
Academy PRO: React native - building first scenes
Academy PRO: React Native - introduction
Academy PRO: Push notifications. Denis Beketsky
Academy PRO: Docker. Lecture 4
Academy PRO: Docker. Lecture 3
Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Lecture 1

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administration Chapter 2
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
Operating system designcfffgfgggggggvggggggggg
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
Reimagine Home Health with the Power of Agentic AI​
Design an Analysis of Algorithms I-SECS-1021-03
history of c programming in notes for students .pptx
Upgrade and Innovation Strategies for SAP ERP Customers
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administration Chapter 2
Which alternative to Crystal Reports is best for small or large businesses.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
How to Choose the Right IT Partner for Your Business in Malaysia
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PTS Company Brochure 2025 (1).pdf.......

Academy PRO: Querying Elasticsearch

  • 1. Querying Elasticsearch Binary Studio Academy PRO 2016 binary-studio.com
  • 4. Search APIs ● Lite (query string) search ● Full-body search
  • 6. http localhost:9200/github/repository/_search?q=language:Javascript%20+forks_count: %3E20000&sort=forks_count:desc&size=3 Lite search Expects all parameters to be passed via query string and encoded properly e.g: http localhost:9200/github/repository/_search?q=name:angular.js Based on _search API: http localhost:9200/_search http localhost:9200/user,repository http localhost:9200/{index}/{type}/_search?q=field:value...
  • 7. http localhost:9200/github/repository/_search?size=2&from=50 Lite search Supports pagination: Supports obligatory conditions (+ -): http localhost:9200/github/repository/_search?q=+language:(php%20css) Supports sorting http localhost:9200/github/repository/_search?q=language:Java&sort=watchers_count:d esc
  • 8. Lite search PROS Powerful Convenient for development and ad- hoc queries End-users can run queries directly from their web-browser CONS Queries should be carefuly encoded Opened API can cause potentially slow queries or even kill your cluster Not so efficient for complex queries
  • 10. FULL-BODY SEARCH ● Utilizes the same _search API ● Transfers parameters in request body e.g curl localhost:9200/github/repository/_search -d '{"size": 2, "from": 10}' ● According to RFC 7231 there is no strict definition what to do when server received GET query with body parameters (depends on server implementation). So both GET and POST methods are allowed. ● Instead of encoded urls there is convenient search query domain-specific language (DSL)
  • 12. SEARCH QUERY CLAUSES ● Leaf clauses - compare field to a query string (match, term, range) ● Compound clauses - combine other query clauses (bool, dis_max)
  • 14. SEARCH QUERY DSL EXAMPLE curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "match": { "language": "Javascript" } } }' curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "bool": { "must": {"match": {"language": "Javascript"}}, "should": {"match": {"description": "library"}} } } }'
  • 15. SEARCH QUERY MATCHERS match multi_match common_terms query_string simple_query_string FULL TEXT QUERIES MATCHERS
  • 16. MATCH & MULTI_MATCH curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "match": { "language": "Javascript" } } }' curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "multi_match": { "query": "javascript", "fields": ["language", "description"] } } }'
  • 17. QUERY STRING QUERY curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "query_string": { "query": "language:(C OR PHP) AND watchers_count:[15000 TO *]" } } }' Supports compact Lucene query string syntax
  • 18. SIMPLE QUERY STRING QUERY curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "simple_query_string": { "fields": ["description"], "query": "(framework^2 realtime) + -(web port client)" } } }' Have simplified query syntax
  • 19. COMMON TERMS QUERY curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "common": { "description": { "query": "for is and web", "cutoff_frequency": 0.001 } } } }' Divides query terms into two groups: ● More important - low frequency ● Less important - high frequency (applied first)
  • 20. SEARCH QUERY FILTERS ● term ● terms ● range ● exists ● missing ● bool ● prefix ● wildcard ● regex ● fuzzy
  • 21. TERM AND RANGE FILTERS curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "term": { "language": "C++" } } }' curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "range": { "watchers_count": { "gte": 5000, "lte": 15000 } } } }'
  • 22. EXISTS AND MISSING FILTERS curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "bool": { "must_not": { "exists": { "field": "language" } } } } } } }'
  • 23. BOOL FILTER ● must ○ Clauses must match, like and ● must_not ○ Clauses must not match, like not ● should ○ At least one of clauses must match, like or .
  • 24. "query": { "filtered": { "query": { "match_all": {} }, "filter": { "bool": { "must": { "term": { "language": "JavaScript" } }, "should": { "range": { "forks_count": { "gt": 10000 } } } } } } }
  • 25. COMBINING FILTERS AND MATCHERS curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "filtered": { "query": { "match": { "has_issues": true } }, "filter": { "term": { "language": "Objective-C" } } } } }'
  • 27. SORTINGcurl localhost:9200/github/repository/_search?pretty -d '{ "query": { "filtered": { "query": { "match": { "has_issues": true } }, "filter": { "term": { "language": "Objective-C" } } } }, "sort": { "forks_count": { "order": "desc" } } }'
  • 29. RELEVANCE ● How well a retrieved document or set of documents meets the information need (criteria) of the user ● Positive FP number stored under _score property ● Calculated by term frequency/inverce document frequency (TF/IDF) algorithm: ○ Term Frequency (tf): more often - more relevant (field) ○ Inverted Document Frequency(idf) more often - less relevant (index) ○ Field-length norm (fieldNorm) shorter - more relevant (field)
  • 30. RELEVANCE EXPLANATION curl localhost:9200/github/repository/_search?pretty -d '{ "query": { "term": { "language": "C++" } }, "size": 1, "explain": true }'

Editor's Notes

  • #16: match query The standard query for performing full text queries, including fuzzy matching and phrase or proximity queries. multi_match query The multi-field version of the match query. common_terms query A more specialized query which gives more preference to uncommon words. query_string query Supports the compact Lucene query string syntax, allowing you to specify AND|OR|NOT conditions and multi-field search within a single query string. For expert users only. simple_query_string A simpler, more robust version of the query_string syntax suitable for exposing directly to users.