ES优化相关

Search as few fields as possible

查询越少的字段越好,通过定义mapping中的copy_to来自动实现拷贝多个字段的值到目标字段,来减少查询的字段数量。

The more fields a query_string or multi_match query targets, the slower it is.

A common technique to improve search speed over multiple fields is to copy their values into a single field at index time,

and then use this field at search time.

This can be automated with the copy-to directive of mappings without having to change the source of documents.

Here is an example of an index containing movies that optimizes queries

that search over both the name and the plot of the movie by indexing both values into the name_and_plot field.

{
  "mappings": {
    "properties": {
      "name_and_plot": {
        "type": "text"
      },
      "name": {
        "type": "text",
        "copy_to": "name_and_plot"
      },
      "plot": {
        "type": "text",
        "copy_to": "name_and_plot"
      }
    }
  }
}

Search rounded dates

关于每时每刻都会变化的事件范围搜索采用约等于的方式搜索,但会导致不精确影响用户体验

In that case we rounded to the minute, so if the current time is 16:31:29, the range query will match everything whose value of the my_date field is between 15:31:00 and 16:31:59.

And if several users run a query that contains this range in the same minute, the query cache could help speed things up a bit.

The longer the interval that is used for rounding, the more the query cache can help, but beware that too aggressive rounding might also hurt user experience.

{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "my_date": {
            "gte": "now-1h/m",
            "lte": "now/m"
          }
        }
      }
    }
  }
}