【问题标题】:Elasticsearch mapping parameters : index vs enabledElasticsearch 映射参数:索引与启用
【发布时间】:2018-11-22 23:49:38
【问题描述】:

我一直在为两个 elasticsearch 映射参数苦苦挣扎:indexenabled。我正在使用 Elasticsearch 6.2.4。


这是我的情况。

映射

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "user_id": {
          "type":  "keyword"
        },
        "last_updated": {
          "type": "date"
        },
        "session_data_index_false": { 
          "index" : false,
          "type" : "keyword"
        },
        "session_data_enabled_false": { 
          "enabled" : false
        }
      }
    }
  }
}

索引

PUT my_index/_doc/1
{
  "user_id": "jpountz",
  "session_data_index_false": "hello", 
  "session_data_enabled_false": "hello", 
  "last_updated": "2015-12-06T18:22:13"
}

搜索1

GET my_index/_search
{
  "query": {
    "match": {
      "session_data_index_false": "hello"
    }
  }
}

我收到以下消息的 400 错误。

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"match\" : {\n    \"session_data_index_false\" : {\n      \"query\" : \"hello\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "6ByxNrjIRQmF23zcmKOvUA",
        "index": "my_index"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my_index",
        "node": "DYPnEJWjTtm58oxZ9F-RSg",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"match\" : {\n    \"session_data_index_false\" : {\n      \"query\" : \"hello\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "6ByxNrjIRQmF23zcmKOvUA",
          "index": "my_index",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [session_data_index_false] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400

搜索2

GET my_index/_search
{
  "query": {
    "match": {
      "session_data_enabled_false": "hello"
    }
  }
}

在这种情况下,我没有收到任何错误。相反,我得到了以下结果,这意味着没有找到任何文件。

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

检索

GET my_index/_doc/1

当然,我可以检索原始数据。

{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "user_id": "jpountz",
    "session_data_index_false": "hello",
    "session_data_enabled_false": "hello",
    "last_updated": "2015-12-06T18:22:13"
  }
}

我阅读了有关上述选项的官方文档。

另外,我读过this article,但发现它与elasticsearch 1.5 兼容。

这里有人知道这两个选项有何不同吗?

提前致谢。

最好的

【问题讨论】:

  • 您是否希望session_data_index_false 上的查询不会出错而只是不返回任何文档?
  • @Val 不。我只想掌握两个选项之间的区别。根据官方文档,当我想为特定字段值禁用索引时,可以使用 index 和 enabeld 选项。但不确定何时使用每个选项以及为什么一个会引发错误而一个不会?

标签: elasticsearch indexing mapping


【解决方案1】:

当将enabled设置为false时,你告诉ES完全忽略该字段的解析,所以它既不会被分析,也不会被索引而不存储(当然_source字段除外)。

因此,ES 甚至不知道该字段存在,因此,它处理这种情况就像您查询任何其他不存在的字段一样,基本上就好像源甚至不包含该字段一样。结果:ES 没有返回任何文档。

当将index 设置为false 时,ES 知道该字段存在(通过映射),但它知道它不应该被索引。因此,当您查询它时,ES 会告诉您不能这样做,因为您决定不索引该字段。这就是 ES 抛出错误的原因,因为您违反了您在映射中声明的合同。

【讨论】:

  • 哦,我明白了。感谢您的及时和适当的回答。
  • 当然,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
相关资源
最近更新 更多