Queries Flashcards

1
Q

How do you do an analysed query (full text)?

A
GET _search
{
   query: {
     match: {
       "text_entry": "< text to search >"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the maximum number of HITS a query can have?

A

10_000 by default.

If you want more you need to use the scroll API.
TODO: What is the scroll API?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the relevancy score?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is the relevancy score calculated?

A

TODO: Learn more about this, is this required for the certification?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the difference between match and match_phrase?

A

Match phrase will match the whole search term, while “match” will match any words in the search term.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What analyser is used by default when doing a query?

A

The analyser specified for the field in the index mappings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to do a simple multi match query?

A
GET _search
{
    query: {
         multi_match: {
            query: "< text to search >",
            fields: ["< field to search >", .... ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the “query string” query type?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you do a term level query?

A
GET _search
{
    query: {
       term: {
          < field name >: {
                value: " < keyword to search > "
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the difference between term and match searches?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you search for multiple terms on the same field?

A
GET _search
{
   query: {
       terms: {
           < field name >: [
                " < value 1 >",
                ....,
                " < value n > ",
           ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you do a numerical range search?

A
GET _search
{
      query: {
          range: {
             "< field name >": {
                  gte: < value >,
                  lte: < value >,

TODO: Learn more about this query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What fields can the range query operate on?

A

numerical and date fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you do a wildcard term query?

A
GET _search
{
    query: {
         wildcard: {
             "< field name >": {
                 value: "< wildcard term >"

TODO: What’s the performance impact on this?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you do a regex term query?

A
GET _search
{
      query: {
          regexp: {
             "< field name >": "< regexp >"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you create compound queries?

A

GET _search
{
query: {
bool: {
must: [< term or match query, etc >],
must_not: [< term or match query etc >],
should: [< term or match query etc >],

17
Q

How does a “must” compound query work?

A

Returns documents that match ALL queries in the must block. (AND query).

18
Q

How does a “must_not” compound query work?

A

Excludes any document that matches the “must_not” block.

19
Q

How does a “should” compound query work?

A

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.

20
Q

How do you make an “OR” query using the “should” block in a compound query?

A

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.

21
Q

How do you use a filter and what is it for?

A

Filters work the same as queries but they don’t affect the relevancy score and are therefore more efficient.

Same syntax as query.

22
Q

How do you name a query and what do you use it for?

A

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.

23
Q

How do you highlight the matching words?

A
GET _search
{
    query: ....,
    highlight: {
         pre_tags: ["< tag1 >],
         post_tags: [" < / tag 2 >"]
         fields: {
             "< field to highlight >": {}
         }
    }

This will add a “highlight” field to the results.

24
Q

How do you sort the results of a query?

A
GET _search
{
    "sort": [
        {
            "< field >": {
              order: "< asc | desc >"

TODO: how does it impact score?

25
Q

How do you do pagination in your queries?

A

It’s paginated by default

GET _search?size=< page size >&from=< offset >

GET _search
{
     size: < page size>,
     from: < offset >,
     query: ....
}
26
Q

What’s the default page size for elastic?

A

10

27
Q

What is the scroll API, how does it work and what do you use it for?

A
  • Search is limited to 10k documents by default.
  • With scroll you set a time window for the search to keep going for a specific amount of time.
# To initiate the scroll
GET _search?scroll=10m&size=< scroll size >

This will return a “scroll_id”

To continue fetching from the scroll

GET _search/scroll
{
scroll: “10m”,
scroll_id: “< scroll id >”

Deleting the scroll

DELETE _search/scroll
{
scroll_id
}

28
Q

What are some best practices for the scroll API?

A
  • Sort your results by id (_doc field)

- Delete the scroll after use

29
Q

How do you close all scrolls at once?

A

DELETE _search/scroll/_all

30
Q

How do you slice your scroll (run in parallel)?

A
GET _search/scroll=10m
{
    slice: {
         id: 0,
         max: < max number of slices >
    }
GET _search/scroll=10m
{
    slice: {
         id: N -1 , # up to the number of slices
         max: < max number of slices >
    }

Create a scroll for each “slice”, so you can fetch in parallel each scroll.

31
Q

How to do fuzzy searches in elastic?

A
# match type
GET _search
{
   query: {
       match: {
           "< field name >": {
                 query: "< text to search >",
                fuzziness: < fuzziness >
          }
# term type
GET _search
{
   query: {
        fuzzy: {
           "< field name >": {
              value: "< value to search > "
             fuzzyness: 1,
             transpositions: true|false

TODO: Read more in the documentation.

32
Q

What does the fuzziness param mean in a fuzzy query?

A

It’s the number of modifications in the original token.

TODO: Read more about this.

33
Q

What is transposition in a fuzzy query? Give an example.

A

Whether to allow characters to transpose (flip)

For example:

transposition = true: "teh" matches "the"
transposition = false: "teh" doesn't match "the"
34
Q

How do you create a template query?

A
# Test the query
GET _search/template
{
    source: {
         query: {
             .....
             value: "{{ param name }}"
         }
    }
    params: {
          "< param name >": "< value >"
    }
}
# Save the query
POST _scripts/< query name to save >
{
   script: {
      lang: "mustache",
      source: {
         ....
     }
   }
}
# Using the query
GET _search/template
{
    id: "< template name >",
    params: {
        ....
    }
}

TODO: What other types of scripts/languages can I create?

35
Q

How do you set default values in a template query?

A

….
“value”: “{{ param }}{{^param}}< default value >{{/param}}”
….

36
Q

How do you perform a remote cluster search?

A

GET < cluster name >:< index name>/_search