0

Here is my current elastic query:

{
    "from": 0,
    "size": 10,
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [{
                        "multi_match": {
                            "query": "ocean",
                            "fields": [],
                            "fuzziness": "AUTO"
                        }}],
                    "must_not": [{
                        "exists": {
                            "field": "parentId"
                        }
                    }]
                }
            },
            "functions" : [
                {
                    "gauss": {
                            "createTime": {
                                    "origin": "2020-07-09T23:50:00",
                                    "scale": "365d",
                                    "decay": 0.3
                            }
                    }
                }
            ]
        }
    }
}

How do I properly add filters to this? I think maybe the fact that I'm using function_score makes this different? I would like to add a hard filter, for example, only show me results with uploadUser: 'Mr. Bean' ... but still keep the scoring in place for the results that pass this filter.

I tried using filter in various places, also using must but I either get no results or all the results.

I'm using Elastic Search 7. Thanks for your help

1
  • can u share your sample data and result which u expect to get ? Commented Jul 15, 2020 at 16:54

1 Answer 1

2

You can try this below search query:

Refer this ES official documentation to know more about Function score query

    {
  "from": 0,
  "size": 10,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "uploadUser": "Mr. Bean"
            }
          },
          "must": [
            {
              "multi_match": {
                "query": "ocean",
                "fields": [
                  
                ],
                "fuzziness": "AUTO"
              }
            }
          ],
          "must_not": [
            {
              "exists": {
                "field": "parentId"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "createTime": {
              "origin": "2020-07-09T23:50:00",
              "scale": "365d",
              "decay": 0.3
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@tyler-g can you try the above search query, and let me know if this works ? And, it would be great if you can provide some sample index data also and your expected result
I tried that way and get no results unfortunately. Without the "filter" I get several results, and I can see some of them have uploadUser: Mr. Bean however as soon as I put the filter on I get no results
@tyler-g can u please share your index data and mapping, and even what search result you expect to get based on that index data ?
@tyler-g did you get a chance to go through my answer, looking forward to get feedback from you :)
Yes got it working thanks for your feedback @Bhavya !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.