Home > Blockchain >  Elasticsearch: How to filter results with a specific word in a value using elasticsearch
Elasticsearch: How to filter results with a specific word in a value using elasticsearch

Time:10-27

I need to add a parameter to my search that filters results containing a specific word in a value. The query is searching for user history records and contains a url key. I need to filter out /history and any other url containing that string.

Here's my current query:

GET /user_log/_search 
{
  "size" : 50,
  "query": {
    "match": {
      "user_id": 56678
    }
  }
}

Here's an example of a record, boiled down to just the value we're looking at:

"_source": {
  "url": "/history?page=2&direction=desc",
},

How can the parameters of the search be changed to filter out this result.

CodePudding user response:

You can use the filter param of boolean query in Elasticsearch. if your url field is of type keyword, you can use the below query

{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "user_id": 56678
                }
            },
            "filter": { --> note filter 
                "term": {
                    "url": "/history"
                }
            }
        }
    }
}

CodePudding user response:

I found a way to solve my specific issue. Instead of filtering on the url I'm filtering on a different value. Here's what I'm using now:

{
  "size" : 50,
  "query": {
    "bool" : {
      "must" : {
        "match" : { "user_id" : 56678 }
      },
      "must_not": {
        "match" : { "controller": "History" }
      }
    }
  }
}

I'm still going to leave this question open for a while to see if anyone has other ways of solving the original problem.

  • Related