How to search/query on docs that have specific file size

hi,
I'm new in elasticsearch and fscrawler.
I want to search only files (docs) that have file size greater than n bytes and less than m byte.
something like this:

GET /_search
{
  "_source": [
    "file.filename", "file.*"
  ],
  
  "query": {
    "query_string": {
      "query": "something"
    },
   
   "_source.file.filesize" : > n and < m

  },

  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

result of search is:

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 63,
    "successful": 63,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 16,
    "max_score": 1.4997041,
    "hits": [
      {
        "_index": "myjob",
        "_type": "doc",
        "_id": "7596d65bd3e1efb6c97f64120ed41c5",
        "_score": 1.4997041,
        "_source": {
          "file": {
            "extension": "html",
            "filename": "indexing.html",
            "content_type": "text/html; charset=UTF-8",
            "indexing_date": "2018-07-08T09:08:49.237+0000",
            "filesize": 96014,
            "last_modified": "2018-07-08T08:56:05.000+0000",
            "url": "file:///tmp/es/indexing.html"
          }
        }
      }
   ]
  }
}

I think you want a range query. I'm on mobile else I'd link you to the docs.

1 Like

hi

  1. Reference - Range datatypes | Elasticsearch Guide [6.4] | Elastic
  2. Examples
  • Example1 - found
    Request

curl -H "Content-type: application/json" localhost:9200/_search -d '
{
"_source": [
"file.filename", "file.*"
],

"query": {
"range": {
"file.filesize": {
"gte" : 95000, "lte": 100000
}
}
}
}'
Response:
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"myjob","_type":"doc","id":"hR-4kmYBZhmfS1VuNCc","_score":1.0,"_source":{"file":{"extension":"html","filename":"indexing.html","content_type":"text/html; charset=UTF-8","indexing_date":"2018-07-08T09:08:49.237+0000","filesize":96014,"last_modified":"2018-07-08T08:56:05.000+0000","url":"file:///tmp/es/indexing.html"}}}]}}kananinja@kananinjapc:~/Documents/PROJECTS/elastic/issue$

- Example2 - not found
Request
curl -H "Content-type: application/json" localhost:9200/_search -d '

{
"_source": [
"file.filename", "file.*"
],

"query": {
"range": {
"file.filesize": {
"gte" : 97000, "lte": 100000
}
}
}
}'
Response
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":}}

1 Like

Thanks guys ...
This link is also useful:
query-string-with-range

The query should be like this:

GET /_search
{
  "_source": [
    "file.*"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "all"
          }
        },
        {
          "range": {
            "file.filesize": {
              "gte": 900,
              "lte": 100000
            }
          }
        }
      ]
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.