Goce Dimkovski
Goce Dimkovski

Reputation: 75

Function score with filters elasticsearch

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?

Upvotes: 6

Views: 6410

Answers (1)

keety
keety

Reputation: 17441

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"
    }
  }
}

Upvotes: 4

Related Questions