【问题标题】:Is it better to query by type in ElasticSearch?在 ElasticSearch 中按类型查询更好吗?
【发布时间】:2016-10-25 18:11:50
【问题描述】:

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

首先,让我们看一下映射

{
  "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"
              },
              .
              .
              .
            }
          } 
        }
      }
    }
  }
}

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

请注意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,因为它只是我正在搜索的文档下的另一个字段。

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

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),会不会是一个不同的答案?

【问题讨论】:

    标签: python performance elasticsearch query-performance


    【解决方案1】:

    elasticsearch 中的类型通过将 _type 属性添加到文档来工作,并且每次搜索特定类型时,它都会自动按 _type 属性过滤。因此,在性能方面应该没有太大区别。类型是一种抽象,而不是实际数据。我的意思是,跨多个文档类型的字段在整个索引上被展平,即一种类型的字段也占用另一种类型的字段的空间,即使它们没有被索引(认为它与 null 占用的方式相同空间)。

    但请务必记住,过滤顺序会影响性能。您必须一次性排除尽可能多的文档。因此,如果您认为最好不要先按类型过滤,则最好先按方式过滤。否则,如果订购相同,我认为不会有太大差异。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 2011-06-07
      • 2021-07-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多