Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search within the results got from elasticsearch

Is it possible to search within the results that I get from elasticsearch?

To achieve that currently I need to run & wait for two searches on elasticsearch: the first search is

{ "match": { "title": "foo" } }

It takes 5 seconds and returns 500 docs etc.. And then a second search

{
  "bool": {
    "must": [
      { "match": { "title": "foo" } },
      { "match": { "title": "bar" } }
    ]
  }
}

It takes another 5 seconds and returns 200 docs, which basically has nothing to do with the first search from elasticsearch's perspective.

Instead of doing it this way, I'd like to offer a "search further within the result" option to my users. Hopefully with this option, users can make a search with more keyword provided based on the result returned from the first search.

So my scenario is that a user makes a first search with keyword "foo", and gets 500 results on the webpage, and then selects "search further within the result", to make a second search within the 500 results, and hope to get some refined results really quick.

How can I achive it? Thanks!

like image 845
John Mccandles Avatar asked Dec 07 '25 18:12

John Mccandles


2 Answers

What you could do is use the IDS query. Collect all document IDs from the first request, and then post them with a new Bool query that includes an IDS query in a must clause next to the original query. You could efficiently collect the IDs in the first request using the Scroll API. Since you will return the second result sorted anyway, it does not make sense to do any sorting in the first request, so you can speed up the first request.

See:

  • Scroll API: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
  • IDS Query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
like image 78
drjz Avatar answered Dec 11 '25 04:12

drjz


post filter is a way to search inside an other search.

In your case :

GET _search
{
  "query": {
    "match": {
      "title": "foo"
    }
  },
  "post_filter": {
    "match": {
      "title": "bar"
    }
  }
}

post_filter will be executed on the query result.

like image 24
Julien TASSIN Avatar answered Dec 11 '25 04:12

Julien TASSIN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!