# How to narrow deep by filter to reduce search time

**URL:** <https://discuss.elastic.co/t/how-to-narrow-deep-by-filter-to-reduce-search-time/69748>\
**Category:** Elasticsearch\
**Created:** [December 22, 2016, 8:08am UTC](https://discuss.elastic.co/t/how-to-narrow-deep-by-filter-to-reduce-search-time/69748 "2016-12-22T08:08:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joomler](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Joomler](https://discuss.elastic.co/u/Joomler)\
**Post date:** [December 22, 2016, 8:08am UTC](https://discuss.elastic.co/t/how-to-narrow-deep-by-filter-to-reduce-search-time/69748/1 "2016-12-22T08:08:26Z")

</div>

I have big search index, i want to narrow deep by filter to reduce search time: so first filter data to search by user project, and then search.  
Now i do search by all data of all user projects - this is very slow.  
From docs i found:

```auto
s = Search()
s = s.filter('terms', tags=['search', 'python'])

```

[http://elasticsearch-dsl.readthedocs.io/en/latest/search\_dsl.html](http://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html)

and i do this in my code ("project" is a field from search\_indexes.py)  
but the following code doesn't find anything (without filter all works)

```auto
        q = Q('bool',
            #must=[Q('match', text='BBQ')],
            should=eval(i), #[Q(...), Q(...)],
            minimum_should_match='1', 
            _name=name_query  
        )
        s = Search(using=client, index="haystack")
        s = s.filter('terms', project=[project_id])
        s = s.query(q).query(~Q("match", text=minus_words))
    
        s.aggs.bucket('per_tag', 'terms', field='project') \
            .metric('max_lines', 'max', field='lines')        
        
        s = s.highlight('text', fragment_size=50)
        s = s[0:1000]
        
        response = s.execute()

```

---

<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:** [January 19, 2017, 8:08am UTC](https://discuss.elastic.co/t/how-to-narrow-deep-by-filter-to-reduce-search-time/69748/2 "2017-01-19T08:08:28Z")

</div>

Closed on January 19, 2017, 8:08am UTC

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