Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Query DSL In Elasticsearch

5,255 views

Published on

Query DSL in Elasticsearch is a way to perform query on elasticsearch cluster.It is rich flexible query language
We can define queries of elasticsearch in JSON format.In this presentation we will see type of query dsl and its usage.

Published in: Software
  • Login to see the comments

Query DSL In Elasticsearch

  1. 1. Narayan Kumar Software Consultant Knoldus Software LLP Query DSL In Elasticsearch
  2. 2. AgendaAgenda ● Overview of Elasticsearch ● What is Query DSL? ● Queries VS Filters ● Type of query ● Demo ● Overview of Elasticsearch ● What is Query DSL? ● Queries VS Filters ● Type of query ● Demo
  3. 3. Overview of ElasticsearchOverview of Elasticsearch Elasticsearch realTime, search & analytics engine open-source distributed multi tenancy scales massively high availability schema free restful API JSON over HTTP lucene based fault tolerance
  4. 4. What is Query DSL ?What is Query DSL ? ➢ It is rich flexible query language. ➢ Elasticsearch provides a full Query DSL based on JSON to define queries. ➢ We can think Query DSL as an AST of queries, consisting of two types of clauses. Leaf query clauses: It looks for a particular value in a particular field, such as the match, term or range queries. Compound query clauses: It wraps other leaf or compound queries and are used to combine multiple queries in a logical fashion.
  5. 5. Queries VS FiltersQueries VS Filters Queries full text search relevance scoring heavier not cacheable Filters exact matching binary yes / no fast cacheable
  6. 6. Type of queryType of query ➢ Match All Query ➢ Full text queries ➢ Term level queries ➢ Compound queries ➢ Match All Query ➢ Full text queries ➢ Term level queries ➢ Compound queries
  7. 7. Match All QueryMatch All Query The most simple query, which matches all documents, giving them all a _score of 1.0. Example: "query": { "match_all": {} }
  8. 8. Full text queriesFull 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. These are full text queries: match_query multi_match query common_terms query query_string query simple_query_string
  9. 9. Full text queries continue ..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" ] } } }
  10. 10. Full text queries continue ..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. query_string query:A query that uses a query parser in order to parse its content. "common": { "body": { "query": "i am spent at starbucks", "cutoff_frequency": 0.001, "low_freq_operator": "and" } } "query": { "query_string": { "query": "(verb:post) AND (body:i am today OR body:came to starbucks)" } }
  11. 11. Full text queries continue ..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" } }
  12. 12. Term level queriesTerm 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. term_query terms_query range_query exists_query prefix_query These are term level queries: wildcard_query regexp_query fuzzy_query type_query ids_query
  13. 13. Term level queries continue….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": { "verb": [ "share", "post" ] } terms_query: Filters documents that have fields that match any of the provided terms.
  14. 14. Term level queries continue….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_query: Returns documents that have at least one non-null value in the original field. "exists": { "field": "actor.links.href" }
  15. 15. Term level queries continue….Term level queries continue…. prefix_query: Matches documents that have fields containing terms with a specified prefix. wildcard_query: Matches documents that have fields matching a wildcard expression . "prefix": { "body": "rt" } "wildcard": { "actor.preferredUsername": "ba*" } "regexp": { "actor.preferredUsername": "ba.*lan" } regexp_query:The regexp query allows you to use regular expression term queries.
  16. 16. Compound QueriesCompound Queries Compound query: Compound queries wrap other compound or leaf queries, either to combine their results and scores, to change their behaviour, or to switch from query to filter context. The queries in this group are: constant_score query bool query dis_max query function_score query boosting query indices query and, or, not filtered query limit query
  17. 17. Compound queries continue….Compound queries continue…. dis_max query: A query that generates the union of documents produced by its subqueries. "dis_max": { "queries": [ { "term": { "verb": "share"}}, { "term": { "verb": "post"}} ] } boosting query:The boosting query can be used to effectively demote results that match a given query. "boosting": { "positive": {"term": { "verb": "post" } }, "negative": { "range": { "actor.friendsCount": {"from": 10,"to": 500 } } }, "negative_boost": 0.5 }
  18. 18. Compound queries continue….Compound queries continue…. bool query: A query that matches documents matching boolean combinations of other queries. "bool" : { "must" : { "term" : { "verb": "post" } }, "filter": { "term" : { "actor.displayName": "rajni" } }, "must_not" : { "range" : { "actor.friendsCount" : { "from" : 10, "to": 500 } } }, "should" : [ { "term" : { "actor.twitterTimeZone": "casablanca" } }, { "term" : { "generator.displayName": "twitter for iPhone" } } ] }
  19. 19. Compound queries continue….Compound queries continue…. constant_score query: A query which wraps another query, but executes it in filter context. All matching documents are given the same “constant” _score. "constant_score": { "filter": { "range": { "actor.friendsCount": { "from": 10, "to": 500 } } } }
  20. 20. Other DSL QueriesOther DSL Queries Joining queries: Performing full SQL-style joins in a distributed system like Elasticsearch . Example: nested_query,has_parent query,has_child query etc. Geo queries: These queries are related to geo_point and geo_shape related operations. Specialized queries: These queries have no any group. It uses for some specific requirement like template_query,script_query etc. Span queries:These are typically used to implement very specific queries on legal documents or patents.
  21. 21. ReferencesReferences https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html MEAP Edition Elasticsearch in Action Version 9
  22. 22. Thank youThank you

×