ES问答101

一、参考

多词查询

match phrase query

Date nanoseconds field typeedit

二、问题列表

2.1 多词查询

场景描述:当一个字段类型为text时候,但是字段值是一个结构化的数据(例如:json), 查询的条件是按照 key: value形式,如何设计查询语句或者分词器?

问题复现:

假设 yz_test索引mapping如下:

GET yz_test/_mapping

# res
{
  "yz_test" : {
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

写入的数据格式如下:

# content字段中是一个结构化的文本字段

POST yz_test/_doc
{
  "content": """
  {
    "name": "dataflux", 
    "level": "high_level", 
    "basic": {
      "age":"xyz"
    }
  }  
  """
}

查询条件: age: xyz & name: dataflux

查询语句如下:

GET yz_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "content": {
              "query": "age : xyz",
              "slop": 0
            }
          }
        },
        {
          "match_phrase": {
            "content": {
              "query": "name : dataflux",
              "slop": 0
            }
          }
        }
      ]
    }
  }
}

查询语句分析:

(1) bool逻辑查询

(2) phrase词组查询

2.2 date字段是否支持纳秒级别?

可以支持,有专有的类型 date_nanos

2.3 聚合后的排序是如何进行的?

2.4 集群无法自动创建索引?

问题描述:报错如下

{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [test]",
        "resource.type" : "index_expression",
        "resource.id" : "test",
        "index_uuid" : "_na_",
        "index" : "test"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [test]",
    "resource.type" : "index_expression",
    "resource.id" : "test",
    "index_uuid" : "_na_",
    "index" : "test"
  },
  "status" : 404
}

问题解决: 参考官方文档 action.auto_create_index

动态配置集群参数action.auto_create_index, 可以自动创建索引, 该值默认为true

PUT _cluster/settings
{
  "persistent": {
    "action.auto_create_index": "true"
  }
}

三、限制

3.1 keyword类型的长度限制

What is the maximum length for keyword type in elasticsearch?

如上, elasticsearch 中的keyword字段类型,最大索引长度为 32766utf-8字符,大约是 32kB, 限制的原因是 lucene中一个term的长度限制为 32766

原文地址:https://www.cnblogs.com/thewindyz/p/13917796.html