gpt4 book ai didi

python - 在 ElasticSearch 中按类型查询更好吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:38 25 4
gpt4 key购买 nike

我的问题是关于性能。我经常使用过滤查询,但我不确定按类型查询的正确方法是什么。

首先,让我们看一下映射:

{
"my_index": {
"mappings": {
"type_Light_Yellow": {
"properties": {
"color_type": {
"properties": {
"color": {
"type": "string",
"index": "not_analyzed"
},
"brightness": {
"type": "string",
"index": "not_analyzed"
}
}
},
"details": {
"properties": {
"FirstName": {
"type": "string",
"index": "not_analyzed"
},
"LastName": {
"type": "string",
"index": "not_analyzed"
},
.
.
.
}
}
}
}
}
}
}

在上面,我们可以看到类型light Yellow 的一个映射示例。此外,还有更多针对各种类型的映射(颜色。例如:深黄色浅棕色 等等...)

请注意color_type 的子字段。对于 type_Light_Yellow 类型,值始终为:"color": "Yellow", "brightness": "Light" 等等所有其他类型。

现在,我的性能问题:我想知道是否有一种最喜欢的方法来查询我的索引。

例如,让我们搜索 "details.FirstName": "John""details.LastName": "Doe" 类型 下的所有文档type_Light_Yellow.

当前方法我正在使用:

curl -XPOST 'http://somedomain.com:1234my_index/_search' -d '{
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"term":{
"color_type.color": "Yellow"
}
},
{
"term":{
"color_type.brightness": "Light"
}
},
{
"term":{
"details.FirstName": "John"
}
},
{
"term":{
"details.LastName": "Doe"
}
}
]
}
}
}
}
}'

从上面可以看出,通过定义"color_type.color": "Yellow""color_type.brightness": "Light",我正在查询所有索引和引用类型 type_Light_Yellow 因为它只是我正在搜索的文档下的另一个字段。

替代方法是直接在类型下查询:

curl -XPOST 'http://somedomain.com:1234my_index/type_Light_Yellow/_search' -d '{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"details.FirstName": "John"
}
},
{
"term": {
"details.LastName": "Doe"
}
}
]
}
}
}
}
}'

请注意第一行:my_index/type_Light_Yellow/_search

  1. 从性能方面来说,什么查询效率更高?
  2. 如果我通过代码查询(我使用的是带有 ElasticSearch 包的 Python),是否会有不同的答案?

最佳答案

elasticsearch 中的类型通过向文档添加 _type 属性来工作,每次您搜索特定类型时,它会自动按 _type 属性进行过滤。因此,在性能方面应该没有太大区别。类型是一种抽象而非实际数据。我在这里的意思是,跨多个文档类型的字段在整个索引上被展平,即一种类型的字段也占用其他类型字段的空间,即使它们没有被索引(可以像 null 一样占用空间)空格)。

但请务必记住,过滤顺序会影响性能。您的目标必须是一次排除尽可能多的文档。所以,如果你认为最好不要先按类型过滤,那么优先过滤是更可取的。否则,如果顺序相同,我认为不会有太大差异。

由于 Python API 在默认设置下也通过 http 进行查询,因此使用 Python 应该不会影响性能。

这里,在您的情况下,尽管在 _type 元字段和颜色字段中都捕获了颜色,但存在一定程度的数据重复。

关于python - 在 ElasticSearch 中按类型查询更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com