0

I'm trying to search across multiple indices with one query, but only apply the gaussian decay function to a field that exists on one of the indices.

I'm running this through elasticsearch-api gem, and that portion works just fine.

Here's the query I'm running in marvel.

GET episodes,shows,keywords/_search?explain
{
"query": {
  "function_score": {
    "query": {
      "multi_match": {
        "query": "AWESOME SAUCE",
        "type": "most_fields",
        "fields": [ "title", "summary", "show_title"]
      }
    },
    "functions": [
      { "boost_factor":  2 },
      {
        "gauss": {
          "published_at": {
            "scale": "4w"
          }
        }
      }
    ],
  "score_mode": "multiply"
  }
},
  "highlight": {
  "pre_tags": ["<span class='highlight'>"],
  "post_tags": ["</span>"],
  "fields": {
    "summary": {},
    "title": {},
    "description": {}
   }
 }
}

The query works great for the episodes index because it has the published_at field for the gauss func to work its magic. However, when run across all indices, it fails for shows and keywords (still succeeds for episodes).

Is it possible to run a conditional gaussian decay function if the published_at field exists or on the single episodes index?

I'm willing to explore alternatives (i.e. run separate queries for each index and then merge the results), but thought a single query would be the best in terms of performance.

Thanks!

daino3
  • 4,386
  • 37
  • 48

2 Answers2

1

You can add a filter to apply those gaussian decay function only to a subset of documents:

{
  "filter": {
    "exists": {
      "field": "published_at"
    }
  }
  "gauss": {
    "published_at": {
      "scale": "4w"
    }
  }
}

For docs that don't have the field you can return a score of 0:

{
  "filter": {
    "missing": {
      "field": "published_at"
    }
  }
  "script_score": {
    "script": "0"
  }
}
Beowulfenator
  • 2,262
  • 16
  • 26
  • Thanks for you answer! I ended up using the [indices](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-indices-query.html) query instead and specifying 2 different queries based on the index. Any idea on pros / cons of that approach vs. your suggested solution? – daino3 Nov 06 '15 at 18:25
  • What's going to happen if a document doesn't have a `published_at` field, even though you expect it to? I bet it will get maximum score, and I don't think you want that. – Beowulfenator Nov 06 '15 at 22:08
  • I guess I never considered that because in our case, documents weren't indexed if they weren't published... but if that's the case this answer is better than our implementation. Danke. – daino3 Nov 09 '15 at 18:22
  • 2
    I think this solution is not working with current version of elasticsearch. I get error about field not present if field is not present in mapping of some indices. – Darshan Patil Feb 24 '18 at 09:08
0

In the newer elasticsearch versions you have to use the script score query. The function score query is getting deprecated.

TheRedhat
  • 473
  • 6
  • 13