# Filter and search through python

**URL:** <https://discuss.elastic.co/t/filter-and-search-through-python/340386>\
**Category:** Elasticsearch\
**Created:** [August 8, 2023, 3:13pm UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386 "2023-08-08T15:13:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IamExperimenting\_Now](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamexperimenting_now/32/122593_2.png) [@IamExperimenting\_Now](https://discuss.elastic.co/u/IamExperimenting_Now)\
**Post date:** [August 8, 2023, 3:13pm UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386/1 "2023-08-08T15:13:07Z")

</div>

Hi,

I'm new to elasticsearch, I'm using elasticsearch for semantic search. I have pushed 5pdf files after converting into vector. when I do search i'm not getting right index value.

so, I thought I would do the filter and search. can someone tell me how to do filter and search

columns names : id, product\_text\_description, product\_vector\_description, product\_name

here, I want to filter first like product\_name=='mobile' and then do search on top of it.

can someone provide/help json query?

I'm using python api to do search

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 8, 2023, 3:48pm UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386/2 "2023-08-08T15:48:49Z")

</div>

Hi @IamExperimenting_Now Welcome to the community

These are big questions

I would suggest the really cool / informative Elastic Labs

> **[GitHub - elastic/elasticsearch-labs: Official Elasticsearch Labs, for Search...](https://github.com/elastic/elasticsearch-labs/tree/main)**
>
> main

There is even a Hybrid Search Example which I think you may be talking about it might be good to look through

> <https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/search/02-hybrid-search.ipynb>

We have a lot of content on this subject

> **[ESRE examples | Elasticsearch Relevance Engine (ESRE) documentation \[8.9\] |...](https://www.elastic.co/guide/en/esre/current/example.html)**

---

<div class="post-metadata">

**Author:** ![IamExperimenting\_Now](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamexperimenting_now/32/122593_2.png) [@IamExperimenting\_Now](https://discuss.elastic.co/u/IamExperimenting_Now)\
**Post date:** [August 9, 2023, 10:30am UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386/3 "2023-08-09T10:30:35Z")

</div>

Hi @stephenb thanks for your reply and article, I went through those elasticsearch lab github pages, but one few .ipynb files are viewable and others are showing "invalid notebook". Anyway thanks for it.

I have question, just want to make sure whether my approach is correct or not.

currently, I have pushed 5 pdf files by creating chunks. Now, I'm planning to do the search after filtering the data inside elasticsearch vector database like rather querying on entire vector database I thought I would do the filter first and then do the search.

is it possible?

```auto
query = {
   "query" : {
        "bool" : {
            "filter" : {
                 "term" : {"product_name" : "mobile"}
          }
     }
  },

"knn" : {
   "field" : "product_vector_description",
   "query_vector" : model.encode("what is the specification mobile model xxxx?"),
   "k" : 3
   } 
}

```

the above query I used to do the search but unfortunately, I'm getting error. could you please help.

Also I tried to split and work, just to check whether I'm able to filter the vector database or not, fortunately, I was able to do the filter.

for that, I used the below query.

```auto
query = {
   "query" : {
        "bool" : {
            "filter" : {
                 "term" : {"product_name" : "mobile"}
          }
     }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 9, 2023, 2:51pm UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386/4 "2023-08-09T14:51:49Z")

</div>

> [@IamExperimenting\_Now](#):
>
> the above query I used to do the search but unfortunately, I'm getting error. could you please help.

We Can't help without the error

I would suggest perhaps going to Kibana Dev - Tools and test your queries there before trying the python

---

<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:** [September 6, 2023, 2:52pm UTC](https://discuss.elastic.co/t/filter-and-search-through-python/340386/5 "2023-09-06T14:52:21Z")

</div>

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