6

I want to have a function, that takes all the results and only orders them if they match the filters. I've tried with these queries but no result. Can someone help with this?

Tryout 1:

{
           "query": {
                "function_score": {
                    "query": {
                        "filtered": {
                            "query": {
                                "match_all": {} 
                            },                        
                            "filter": {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "category_boats": "motor_boat"
                                            }
                                        }                                           
                                    ]
                                }
                            }
                        }
                    },
                    "functions": [

                    ],
                    "score_mode": "first"
                }
            }
        }

Tryout 2:

{
           "query": {
                "function_score": {
                    "query": { 
                        "match_all": {}
                    },
                    "functions": [ 
                        {
                            "filter": { 
                                "term": { 
                                    "boat_type": "offshore_yacht" 
                                }
                            }, 
                            "weight": 1
                        },
                        {
                            "filter": { 
                                "term": { 
                                    "year_built": "2016" 
                                }
                            }, 
                            "weight": 1
                        },
                        {
                            "filter": { 
                                "term": { 
                                    "manufacturer": "MasterCraft" 
                                }
                            }, 
                            "weight": 2 
                        }
                    ],
                    "score_mode": "first"
                }
            }
        }

On the last code I get an error: No function with the name [weight] is registered.] So is weight for filter supported or not? Some help?

4
  • which version of elasticsearch ? Commented Feb 15, 2016 at 16:24
  • Version: "number" : "1.3.2". But if this is a problem I can update the ElasticSearch server Commented Feb 17, 2016 at 10:11
  • curious doesn't the below approach work? Commented Feb 19, 2016 at 20:49
  • I works the solutions bellow. Thanks Keety Commented Feb 25, 2016 at 15:53

1 Answer 1

4

Support for weight was added in elasticsearch 1.4 prior to that it was referred to as boost_factor

Example:

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "term": {
              "boat_type": "offshore_yacht"
            }
          },
          "boost_factor": 1
        },
        {
          "filter": {
            "term": {
              "year_built": "2016"
            }
          },
          "boost_factor": 1
        },
        {
          "filter": {
            "term": {
              "manufacturer": "MasterCraft"
            }
          },
          "boost_factor": 2
        }
      ],
      "score_mode": "first"
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Keety. It works. I didn't know that "weight" is only for the Elastic after 1.4. I was always confused in the tutorials and examples why there is sometimes "weight" and sometimes "boost_factor"
I have difficulty implementing this in Elastic "2.2.0". They changed something on how the filtering is done. It's not simply replacing "boost_factor" with "weight". Can someone help?
just tried the above query in answer and works fine for me in elasticsearch 2.2.0 . Could you file another question with the actual query that you are executing and the problem you are facing in 2.2.0
This query above lists the results - true, but they are not sorted by the boost_factor or weight like they are in Elasticsearch 1.3.2. It shown the results, from "match_all": {}, but not sorted anything we want by the filters like version 1.3.2. It must be some other way...

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.