Elasticsearch - search across multiple indices with conditional decay function

我与影子孤独终老i 提交于 2019-12-12 06:04:53

问题


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!


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


来源:https://stackoverflow.com/questions/28589015/elasticsearch-search-across-multiple-indices-with-conditional-decay-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!