Elasticsearch DSL query - Get all matching results

nirmalraj17

I am trying to search an index using DSL query. I have many documents which matches the criteria of log and the range of timestamp.
I am passing dates and converting it to epoch milli seconds.
But I am specifying size parameter in DSL query.
What I see is that if I specify 5000, it extracts 5000 records in the time range. But there are more number of records in the specified time range.
How to retrieve all data matching the range of time so that I dont need to specify the size?

My DSL query is as below.

GET localhost:9200/_search    
{
    "query": {
      "bool": {
        "must": [
          {"match_phrase": {
              "log":  "SOME_VALUE"
              }
            },
             {"range": {
                "@timestamp": {
                  "gte": "'"${fromDate}"'", 
                  "lte": "'"${toDate}"'", 
                  "format": "epoch_millis"
                }
              }
            }
                ]
              }
            },    
        "size":5000
}

fromDate = 1519842600000
toDate = 1520533800000

nirmalraj17

I couldn't get the scan API or scroll pattern working as it was also not showing expected result.

I finally figured out a way to capture the number of hits and then pass that as parameter to extract the data.

GET localhost:9200/_count    
{
"query": {
  "bool": {
    "must": [
      {"match_phrase": {
          "log":  "SOME_VALUE"
          }
        },
         {"range": {
            "@timestamp": {
              "gte": "'"${fromDate}"'", 
              "lte": "'"${toDate}"'", 
              "format": "epoch_millis"
            }
          }
        }
            ]
          }
        }
}' > count_size.txt
size_count=`cat count_size.txt  | cut -d "," -f1 | cut -d ":" -f2`
echo "Total hits matching this criteria is ${size_count}"

From this I get the size_count value. If this value is less than 10000, extract the value, else reduce the time range for extraction.

GET localhost:9200/_search    
{
"query": {
  "bool": {
    "must": [
      {"match_phrase": {
          "log":  "SOME_VALUE"
          }
        },
         {"range": {
            "@timestamp": {
              "gte": "'"${fromDate}"'", 
              "lte": "'"${toDate}"'", 
              "format": "epoch_millis"
            }
          }
        }
            ]
          }
        },    
    "size":'"${size_count}"'
}

If large set of data is required for an extensive period, I need to run this with a different set of dates and combine them together to get an overall required reports.

This complete piece of code is written is shell script so I am able to use it much simpler.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is ElasticSearch match query returning all results?

From Dev

Elasticsearch DSL Query for Update

From Dev

How to Get All Results from Elasticsearch in Python

From Dev

Get all rows matching a list in a SQL query

From Dev

MySQL: Update all rows in 2 table matching results of another query

From Dev

Elasticsearch DSL query with specific Output

From Dev

Mix DSL and URI query in Elasticsearch

From Dev

Range or missing - ElasticSearch - Query DSL

From Dev

Get all fields of a document in ElasticSearch search query

From Dev

ElasticSearch: getting facets from all results with filter query

From Dev

Get results for sub query with matching values from a List

From Dev

fetch data from and to date to get all matching results

From Dev

Elasticsearch query yeilds no results

From Dev

Are ElasticSearch query results cached?

From Dev

How to paginate results from Elasticsearch DSL in Python

From Dev

What is the most efficient way to get all results from Elasticsearch?

From Dev

SQL query to get all rows when there is no matching row in left join

From Dev

Elasticsearch Multiple Prefix query OR Matching

From Dev

Confusions about the Elasticsearch json dsl query structure

From Dev

Elasticsearch DSL query from an SQL statement

From Dev

elasticsearch-dsl-py query formation

From Dev

Django-Elasticsearch-DSL Nested Range Query

From Dev

How to rewrite ElasticSearch DSL query with the Java API

From Dev

Using Elasticsearch DSL Query with Hadoop Mapreduce

From Dev

Using elasticsearch dsl query with multiple indices

From Dev

Mapping logical queries to elasticsearch query DSL

From Dev

How to write java code for elasticsearch DSL query

From Dev

Query DSL elasticsearch doesn't work

From Dev

Exclude term in elasticsearch_dsl filter query

Related Related

  1. 1

    Why is ElasticSearch match query returning all results?

  2. 2

    Elasticsearch DSL Query for Update

  3. 3

    How to Get All Results from Elasticsearch in Python

  4. 4

    Get all rows matching a list in a SQL query

  5. 5

    MySQL: Update all rows in 2 table matching results of another query

  6. 6

    Elasticsearch DSL query with specific Output

  7. 7

    Mix DSL and URI query in Elasticsearch

  8. 8

    Range or missing - ElasticSearch - Query DSL

  9. 9

    Get all fields of a document in ElasticSearch search query

  10. 10

    ElasticSearch: getting facets from all results with filter query

  11. 11

    Get results for sub query with matching values from a List

  12. 12

    fetch data from and to date to get all matching results

  13. 13

    Elasticsearch query yeilds no results

  14. 14

    Are ElasticSearch query results cached?

  15. 15

    How to paginate results from Elasticsearch DSL in Python

  16. 16

    What is the most efficient way to get all results from Elasticsearch?

  17. 17

    SQL query to get all rows when there is no matching row in left join

  18. 18

    Elasticsearch Multiple Prefix query OR Matching

  19. 19

    Confusions about the Elasticsearch json dsl query structure

  20. 20

    Elasticsearch DSL query from an SQL statement

  21. 21

    elasticsearch-dsl-py query formation

  22. 22

    Django-Elasticsearch-DSL Nested Range Query

  23. 23

    How to rewrite ElasticSearch DSL query with the Java API

  24. 24

    Using Elasticsearch DSL Query with Hadoop Mapreduce

  25. 25

    Using elasticsearch dsl query with multiple indices

  26. 26

    Mapping logical queries to elasticsearch query DSL

  27. 27

    How to write java code for elasticsearch DSL query

  28. 28

    Query DSL elasticsearch doesn't work

  29. 29

    Exclude term in elasticsearch_dsl filter query

HotTag

Archive