Introduction to Elasticsearch Searching

13
INTRODUCTION TO SEARCHING by Bo Andersen - codingexplained.com

Transcript of Introduction to Elasticsearch Searching

Page 1: Introduction to Elasticsearch Searching

INTRODUCTION TO SEARCHING

by Bo Andersen - codingexplained.com

Page 2: Introduction to Elasticsearch Searching

OUTLINE➤ Relevancy & scoring➤ Ways of searching

➤ Query string➤ Query DSL

➤ Types of queries➤ Leaf & compound➤ Full text➤ Term level➤ Compound

Page 3: Introduction to Elasticsearch Searching

RELEVANCY & SCORING➤ To rank documents for a query, a score is calculated for each document

that matches a query➤ The higher the score, the more relevant the document is to the search

query➤ Queries in query context affect the scores of matching documents

➤ "How well does the document match?"➤ Queries in filter context do not affect the scores of matching documents

➤ "Does the document match"

Page 4: Introduction to Elasticsearch Searching

WAYS OF SEARCHING

Page 5: Introduction to Elasticsearch Searching

QUERY STRING➤ Search by sending search parameters through the REST request URI (as

query parameters)➤ Used for simple queries and ad-hoc queries on the command line➤ Also supports advanced queries➤ Example

➤ GET http://localhost:9200/ecommerce/product/_search?q=pasta

Page 6: Introduction to Elasticsearch Searching

QUERY DSL➤ Search by defining queries within the request body in JSON➤ Supports more features than the query string approach➤ Used for more advanced queries➤ Often easier to read, as queries are defined in JSON

GET http://localhost:9200/ecommerce/product/_search

{

"query": {

"match": {

"name": "pasta"

}

}

}

Page 7: Introduction to Elasticsearch Searching

TYPES OF QUERIES

Page 8: Introduction to Elasticsearch Searching

LEAF & COMPOUND QUERIES➤ Leaf

➤ Look for particular values in particular fields, for instance "pasta" in product names

➤ Can be used by themselves within a query, without being part of a compound query

➤ Can also be used within compound queries to construct more advanced queries

➤ Compound➤ Wrap leaf clauses or even other compound query clauses➤ Used to combine multiple queries in a logical fashion (usually with

boolean logic)➤ Can also be used to alter the behavior of queries

Page 9: Introduction to Elasticsearch Searching

FULL TEXT➤ Used for running full text queries on full text fields

➤ E.g. a product name or description➤ Values are analyzed when adding documents or modifying values

➤ E.g. removing stop words, tokenizing and lowercasing➤ Will apply each field's analyzer to the query string before executing

Page 10: Introduction to Elasticsearch Searching

TERM LEVEL➤ Used for exact matching of values➤ Usually used for structured data like numbers and dates, rather than full

text fields➤ E.g. finding persons born between year 1980 and 2000

➤ Search queries are not analyzed before executing

Page 11: Introduction to Elasticsearch Searching

JOINING QUERIES➤ Performing joins in a distributed system is expensive➤ Elasticsearch offers two forms of joins that are designed to scale horizontally➤ Nested query

➤ Documents may contains fields of type nested with arrays of objects➤ Each object can be queried with the nested query as an independent document

➤ has_child and has_parent queries➤ A parent-child relationship can exist between two document types within a

single index➤ The has_child query returns parent documents whose child documents match

the query➤ The has_parent query returns child documents whose parent document

matches the query

Page 12: Introduction to Elasticsearch Searching

GEO QUERIES➤ Elasticsearch supports two types of geo fields

➤ geo_point (lat/lon pairs)➤ geo_shape (points, lines, circles, polygons, etc.)

➤ Various geo queries use these fields to perform geographical searches➤ E.g. finding points of interest near GPS coordinates

Page 13: Introduction to Elasticsearch Searching

THANK YOU FOR WATCHING!