Professional Documents
Culture Documents
Query DSL in Elasticsearch: Narayan Kumar Software Consultant Knoldus Software LLP
Query DSL in Elasticsearch: Narayan Kumar Software Consultant Knoldus Software LLP
Narayan Kumar
Software Consultant
Knoldus Software LLP
Agenda
● Overview of Elasticsearch
● Queries VS Filters
● Type of query
● Demo
Overview of Elasticsearch
fault tolerance
lucene based
restful API
JSON over HTTP
What is Query DSL ?
Queries Filters
heavier fast
➢ Compound queries
Match All Query
Example:
"query": {
"match_all": {}
}
Full text queries
The high-level full text queries are usually used for running full text queries
on full text fields like the body of an email.
match_query
multi_match query
common_terms query
query_string query
simple_query_string
Full text queries continue ..
match_query: A family of match queries that accepts text/numerics/dates, analyzes
them, and constructs a query.
{
"query": {
"match": {
"body": {
"query": "i spent at starbucks",
"operator": "and"
}
}
}
}
multi_match:The multi_match query builds on the match query to allow multi-field
queries {
"query": {
"multi_match": {
"query": "share post",
"fields": [
"verb"
]
}
}
}
Full text queries continue ..
common_terms query: The common terms query is a modern alternative to
stopwords which improves the precision and recall of search results (by taking
stopwords into account), without sacrificing performance.
"common": {
"body": {
"query": "i am spent at starbucks",
"cutoff_frequency": 0.001,
"low_freq_operator": "and"
}
}
query_string query:A query that uses a query parser in order to parse its content.
"query": {
"query_string": {
"query": "(verb:post) AND (body:i am today OR body:came to starbucks)"
}
}
Full text queries continue ..
simple_query_string query: A query that uses the SimpleQueryParser to parse its
context.The simple_query_string query will never throw an exception, and discards
invalid parts of the query.
"query": {
"simple_query_string": {
"query": "\"at starbucks\" | today -starbucks",
"fields": [
"body"
],
"flags": "OR|NOT|PHRASE"
}
}
Term level queries
The term-level queries operate on the exact terms that are stored in the
inverted index.These queries are usually used for structured data like
numbers, dates, and enums, rather than full text fields.
These are term level queries:
term_query wildcard_query
terms_query regexp_query
range_query fuzzy_query
exists_query type_query
prefix_query ids_query
Term level queries continue….
term_query: The term query finds documents that contain the exact term
specified in the inverted index.
"term": {
"actor.postedTime": "2010-11-17T03:55:57.000Z"
}
terms_query: Filters documents that have fields that match any of the
provided terms.
"terms": {
"verb": [
"share",
"post"
]
}
Term level queries continue….
range_query: Matches documents with fields that have terms within a
certain range.
"range": {
"actor.friendsCount": {
"gte": 10,
"lte": 500
}
}
"exists": {
"field": "actor.links.href"
}
Term level queries continue….
prefix_query: Matches documents that have fields containing terms with a
specified prefix.
"prefix": {
"body": "rt"
}
wildcard_query: Matches documents that have fields matching a wildcard
expression .
"wildcard": {
"actor.preferredUsername": "ba*"
}
"constant_score": {
"filter": {
"range": {
"actor.friendsCount": {
"from": 10,
"to": 500
}
}
}
}
Other DSL Queries
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html