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

**URL:** <https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253>\
**Category:** Elasticsearch\
**Created:** [October 20, 2018, 4:27pm UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253 "2018-10-20T16:27:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohsen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsen/32/42864_2.png) [@mohsen](https://discuss.elastic.co/u/mohsen)\
**Post date:** [October 20, 2018, 4:27pm UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253/1 "2018-10-20T16:27:27Z")

</div>

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"
          }
        }
      }
   ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 20, 2018, 5:11pm UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253/2 "2018-10-20T17:11:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)\
**Post date:** [October 20, 2018, 6:33pm UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253/3 "2018-10-20T18:33:16Z")

</div>

> [@mohsen](#):
>
> "\_source": ["file.filename", "file.\*"], "query": { "query\_string": { "query": "something" },

hi

1. Reference - [Range datatypes | Elasticsearch Guide [6.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/range.html)
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":}}

---

<div class="post-metadata">

**Author:** ![mohsen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsen/32/42864_2.png) [@mohsen](https://discuss.elastic.co/u/mohsen)\
**Post date:** [October 21, 2018, 7:55am UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253/4 "2018-10-21T07:55:01Z")

</div>

Thanks guys ...  
This link is also useful:  
[query-string-with-range](https://discuss.elastic.co/t/query-string-with-range/85111/2)

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"
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 18, 2018, 7:55am UTC](https://discuss.elastic.co/t/how-to-search-query-on-docs-that-have-specific-file-size/153253/5 "2018-11-18T07:55:03Z")

</div>

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