Queries Flashcards
How do you do an analysed query (full text)?
GET _search { query: { match: { "text_entry": "< text to search >"
What’s the maximum number of HITS a query can have?
10_000 by default.
If you want more you need to use the scroll API.
TODO: What is the scroll API?
What is the relevancy score?
A score of how relevant a document is given an analysed search.
This only affects analysed searches (match query), because it wouldn’t make sense for term queries.
How is the relevancy score calculated?
TODO: Learn more about this, is this required for the certification?
What’s the difference between match and match_phrase?
Match phrase will match the whole search term, while “match” will match any words in the search term.
What analyser is used by default when doing a query?
The analyser specified for the field in the index mappings.
How to do a simple multi match query?
GET _search { query: { multi_match: { query: "< text to search >", fields: ["< field to search >", .... ]
What is the “query string” query type?
Performs a query with “query string” style params. Allows booleans operation in the query like “this OR that”.
GET _search
{
query: {
“query_string”: {
default_field: “< field to search >”,
query: “< boolean query syntax >”
TODO: Learn more about this query DSL.
How do you do a term level query?
GET _search { query: { term: { < field name >: { value: " < keyword to search > "
What’s the difference between term and match searches?
Term searches match the whole field is is used with “keyword” type fields, while match queries are analysed: the text is tokenized and intersected with the analysed field in the document, thus allowing partial matches.
How do you search for multiple terms on the same field?
GET _search { query: { terms: { < field name >: [ " < value 1 >", ...., " < value n > ", ]
How do you do a numerical range search?
GET _search { query: { range: { "< field name >": { gte: < value >, lte: < value >,
TODO: Learn more about this query.
What fields can the range query operate on?
numerical and date fields.
How do you do a wildcard term query?
GET _search { query: { wildcard: { "< field name >": { value: "< wildcard term >"
TODO: What’s the performance impact on this?
How do you do a regex term query?
GET _search { query: { regexp: { "< field name >": "< regexp >"
How do you create compound queries?
GET _search
{
query: {
bool: {
must: [< term or match query, etc >],
must_not: [< term or match query etc >],
should: [< term or match query etc >],
How does a “must” compound query work?
Returns documents that match ALL queries in the must block. (AND query).
How does a “must_not” compound query work?
Excludes any document that matches the “must_not” block.
How does a “should” compound query work?
Provides optional queries to match that will increase the relevancy score, but they are not required to be present in the document. Only affects the score.
How do you make an “OR” query using the “should” block in a compound query?
Set “minimum_should_match” option. This will required that at least N of those queries match. If you set it to 1 it will effectively work an “OR” type of query.
How do you use a filter and what is it for?
Filters work the same as queries but they don’t affect the relevancy score and are therefore more efficient.
Same syntax as query.
How do you name a query and what do you use it for?
Add it to your query like so:
“term”: {
“field”: {
“_name”: “< query name >”,
“value”: ….
This will add a “matched_queries” field to your search results so you know which queries matches each document.
How do you highlight the matching words?
GET _search { query: ...., highlight: { pre_tags: ["< tag1 >], post_tags: [" < / tag 2 >"] fields: { "< field to highlight >": {} } }
This will add a “highlight” field to the results.
How do you sort the results of a query?
GET _search { "sort": [ { "< field >": { order: "< asc | desc >"
TODO: how does it impact score?