脚本、缓存和搜索速度

编辑

Elasticsearch 执行了许多优化措施,以尽可能提高使用脚本的速度。其中一个重要的优化是脚本缓存。编译后的脚本会被放入缓存中,这样引用该脚本的请求就不会产生编译开销。

缓存大小非常重要。您的脚本缓存应该足够大,以容纳用户需要同时访问的所有脚本。

如果您在节点统计中看到大量的脚本缓存驱逐和编译次数上升,则说明您的缓存可能太小。

默认情况下,所有脚本都会被缓存,因此只有在更新发生时才需要重新编译。默认情况下,脚本没有基于时间的过期时间。您可以使用 script.cache.expire 设置来更改此行为。使用 script.cache.max_size 设置来配置缓存的大小。

脚本的大小限制为 65,535 字节。设置 script.max_size_in_bytes 的值可以增加这个软限制。如果您的脚本非常大,请考虑使用原生脚本引擎

提高搜索速度

编辑

脚本非常有用,但不能使用 Elasticsearch 的索引结构或相关优化。这种关系有时会导致搜索速度变慢。

如果您经常使用脚本来转换索引数据,则可以通过在摄取期间转换数据来加快搜索速度。但是,这通常意味着索引速度会变慢。让我们看一个实际的例子来说明如何提高搜索速度。

在运行搜索时,通常会按两个值的总和对结果进行排序。例如,考虑一个名为 my_test_scores 的索引,其中包含测试分数数据。此索引包含两个类型为 long 的字段

  • math_score
  • verbal_score

您可以运行一个使用脚本将这些值相加的查询。这种方法没有错,但是由于脚本估值发生在请求过程中,因此查询速度会较慢。以下请求返回 grad_year 等于 2099 的文档,并按脚本的估值结果排序。

resp = client.search(
    index="my_test_scores",
    query={
        "term": {
            "grad_year": "2099"
        }
    },
    sort=[
        {
            "_script": {
                "type": "number",
                "script": {
                    "source": "doc['math_score'].value + doc['verbal_score'].value"
                },
                "order": "desc"
            }
        }
    ],
)
print(resp)
response = client.search(
  index: 'my_test_scores',
  body: {
    query: {
      term: {
        grad_year: '2099'
      }
    },
    sort: [
      {
        _script: {
          type: 'number',
          script: {
            source: "doc['math_score'].value + doc['verbal_score'].value"
          },
          order: 'desc'
        }
      }
    ]
  }
)
puts response
const response = await client.search({
  index: "my_test_scores",
  query: {
    term: {
      grad_year: "2099",
    },
  },
  sort: [
    {
      _script: {
        type: "number",
        script: {
          source: "doc['math_score'].value + doc['verbal_score'].value",
        },
        order: "desc",
      },
    },
  ],
});
console.log(response);
GET /my_test_scores/_search
{
  "query": {
    "term": {
      "grad_year": "2099"
    }
  },
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": "doc['math_score'].value + doc['verbal_score'].value"
        },
        "order": "desc"
      }
    }
  ]
}

如果您正在搜索小型索引,则将脚本包含在搜索查询中可能是一个不错的解决方案。如果您想加快搜索速度,可以在摄取期间执行此计算,并将总和索引到字段中。

首先,我们将在索引中添加一个名为 total_score 的新字段,其中将包含 math_scoreverbal_score 字段值的总和。

resp = client.indices.put_mapping(
    index="my_test_scores",
    properties={
        "total_score": {
            "type": "long"
        }
    },
)
print(resp)
response = client.indices.put_mapping(
  index: 'my_test_scores',
  body: {
    properties: {
      total_score: {
        type: 'long'
      }
    }
  }
)
puts response
const response = await client.indices.putMapping({
  index: "my_test_scores",
  properties: {
    total_score: {
      type: "long",
    },
  },
});
console.log(response);
PUT /my_test_scores/_mapping
{
  "properties": {
    "total_score": {
      "type": "long"
    }
  }
}

接下来,使用包含脚本处理器摄取管道来计算 math_scoreverbal_score 的总和,并将其索引到 total_score 字段中。

resp = client.ingest.put_pipeline(
    id="my_test_scores_pipeline",
    description="Calculates the total test score",
    processors=[
        {
            "script": {
                "source": "ctx.total_score = (ctx.math_score + ctx.verbal_score)"
            }
        }
    ],
)
print(resp)
response = client.ingest.put_pipeline(
  id: 'my_test_scores_pipeline',
  body: {
    description: 'Calculates the total test score',
    processors: [
      {
        script: {
          source: 'ctx.total_score = (ctx.math_score + ctx.verbal_score)'
        }
      }
    ]
  }
)
puts response
const response = await client.ingest.putPipeline({
  id: "my_test_scores_pipeline",
  description: "Calculates the total test score",
  processors: [
    {
      script: {
        source: "ctx.total_score = (ctx.math_score + ctx.verbal_score)",
      },
    },
  ],
});
console.log(response);
PUT _ingest/pipeline/my_test_scores_pipeline
{
  "description": "Calculates the total test score",
  "processors": [
    {
      "script": {
        "source": "ctx.total_score = (ctx.math_score + ctx.verbal_score)"
      }
    }
  ]
}

要更新现有数据,请使用此管道将 my_test_scores 中的任何文档重新索引到名为 my_test_scores_2 的新索引中。

resp = client.reindex(
    source={
        "index": "my_test_scores"
    },
    dest={
        "index": "my_test_scores_2",
        "pipeline": "my_test_scores_pipeline"
    },
)
print(resp)
response = client.reindex(
  body: {
    source: {
      index: 'my_test_scores'
    },
    dest: {
      index: 'my_test_scores_2',
      pipeline: 'my_test_scores_pipeline'
    }
  }
)
puts response
const response = await client.reindex({
  source: {
    index: "my_test_scores",
  },
  dest: {
    index: "my_test_scores_2",
    pipeline: "my_test_scores_pipeline",
  },
});
console.log(response);
POST /_reindex
{
  "source": {
    "index": "my_test_scores"
  },
  "dest": {
    "index": "my_test_scores_2",
    "pipeline": "my_test_scores_pipeline"
  }
}

继续使用该管道将任何新文档索引到 my_test_scores_2

resp = client.index(
    index="my_test_scores_2",
    pipeline="my_test_scores_pipeline",
    document={
        "student": "kimchy",
        "grad_year": "2099",
        "math_score": 1200,
        "verbal_score": 800
    },
)
print(resp)
response = client.index(
  index: 'my_test_scores_2',
  pipeline: 'my_test_scores_pipeline',
  body: {
    student: 'kimchy',
    grad_year: '2099',
    math_score: 1200,
    verbal_score: 800
  }
)
puts response
const response = await client.index({
  index: "my_test_scores_2",
  pipeline: "my_test_scores_pipeline",
  document: {
    student: "kimchy",
    grad_year: "2099",
    math_score: 1200,
    verbal_score: 800,
  },
});
console.log(response);
POST /my_test_scores_2/_doc/?pipeline=my_test_scores_pipeline
{
  "student": "kimchy",
  "grad_year": "2099",
  "math_score": 1200,
  "verbal_score": 800
}

这些更改会减慢索引过程,但可以加快搜索速度。您可以使用 total_score 字段对 my_test_scores_2 上进行的搜索进行排序,而不是使用脚本。响应几乎是实时的!尽管此过程会减慢摄取时间,但它会大大提高搜索时的查询速度。

resp = client.search(
    index="my_test_scores_2",
    query={
        "term": {
            "grad_year": "2099"
        }
    },
    sort=[
        {
            "total_score": {
                "order": "desc"
            }
        }
    ],
)
print(resp)
response = client.search(
  index: 'my_test_scores_2',
  body: {
    query: {
      term: {
        grad_year: '2099'
      }
    },
    sort: [
      {
        total_score: {
          order: 'desc'
        }
      }
    ]
  }
)
puts response
const response = await client.search({
  index: "my_test_scores_2",
  query: {
    term: {
      grad_year: "2099",
    },
  },
  sort: [
    {
      total_score: {
        order: "desc",
      },
    },
  ],
});
console.log(response);
GET /my_test_scores_2/_search
{
  "query": {
    "term": {
      "grad_year": "2099"
    }
  },
  "sort": [
    {
      "total_score": {
        "order": "desc"
      }
    }
  ]
}