Function_score script_score query vs sort script performance


(Dmitriy Olshanskiy) #1

I've found an order of magnitude difference in performance of queries that use function_score with script-based score vs queries that use sort with the same script.
What could cause this difference ? Does it make sense to stick to creative use of custom_score for sorting?
(I'm using ES 1.7.4)

Example queries:

{
  "explain": false,
  "fields": [
    "sku"
  ],
  "from": 0,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "term": {
              "searchable": "1"
            }
          },
          "must_not": [
            {
              "term": {
                "product_invisible": "1"
              }
            },
            {
              "terms": {
                "product_visibility": [
                  "1",
                  "3"
                ]
              }
            }
          ]
        }
      },
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "boost": 2,
              "minimum_should_match": "1",
              "should": [
                {
                  "multi_match": {
                    "fields": [
                      "attributes_brand_3^2.000000",
                      "category^2.000000",
                      "name^1.500000",
                      "attributes_author_131^1.000000",
                      "keywords_field^2.500000",
                      "skus^5.000000"
                    ],
                    "minimum_should_match": "2<-1 5<80%",
                    "query": "iphone 6 4.7 case",
                    "slop": 20,
                    "tie_breaker": 1,
                    "type": "cross_fields",
                    "use_dis_max": true
                  }
                },
                {
                  "multi_match": {
                    "fields": [
                      "name^1.500000",
                      "category^2.000000",
                      "attributes_brand_3^2.000000",
                      "keywords_field^2.500000",
                      "attributes_author_131^1.000000",
                      "skus^5.000000"
                    ],
                    "operator": "AND",
                    "query": "iphone 6 4.7 case",
                    "slop": 20,
                    "tie_breaker": 1,
                    "type": "phrase",
                    "use_dis_max": true
                  }
                }
              ]
            }
          },
          "script_score": {
            "lang": "groovy",
            "script": "doc['facet_price'].value - 10000"
          },
          "boost_mode" : "replace"
        }
      }
    }
  },
  "size": 20
}

vs


{
  "explain": false,
  "fields": [
    "sku"
  ],
  "from": 0,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "term": {
              "searchable": "1"
            }
          },
          "must_not": [
            {
              "term": {
                "product_invisible": "1"
              }
            },
            {
              "terms": {
                "product_visibility": [
                  "1",
                  "3"
                ]
              }
            }
          ]
        }
      },
      "query": {
        "bool": {
          "boost": 2,
          "minimum_should_match": "1",
          "should": [
            {
              "multi_match": {
                "fields": [
                  "attributes_brand_3^2.000000",
                  "category^2.000000",
                  "name^1.500000",
                  "attributes_author_131^1.000000",
                  "keywords_field^2.500000",
                  "skus^5.000000"
                ],
                "minimum_should_match": "2<-1 5<80%",
                "query": "iphone 6 4.7 case",
                "slop": 20,
                "tie_breaker": 1,
                "type": "cross_fields",
                "use_dis_max": true
              }
            },
            {
              "multi_match": {
                "fields": [
                  "name^1.500000",
                  "category^2.000000",
                  "attributes_brand_3^2.000000",
                  "keywords_field^2.500000",
                  "attributes_author_131^1.000000",
                  "skus^5.000000"
                ],
                "operator": "AND",
                "query": "iphone 6 4.7 case",
                "slop": 20,
                "tie_breaker": 1,
                "type": "phrase",
                "use_dis_max": true
              }
            }
          ]
        }
      }
    }
  },
  "size": 20,
  "sort": {
      "_script" : {
          "type" : "number",
          "script" : {
              "lang": "groovy",
              "script": "doc['facet_price'].value - 10000"
          }
          
      }
  },
  "timeout": "1s"
}

Performance of NativeScripts
(system) #2