如何将所有字段的Elasticsearch索引映射设置为not_analysed

时间:2019-03-18 05:55:42

标签: elasticsearch

我希望我的elasticsearch索引与所有字段的精确值匹配。如何将所有字段的索引都映射到“ not_analysed”。

2 个答案:

答案 0 :(得分:0)

您可以为此使用dynamic_templates映射。默认情况下,Elasticsearch使字段类型分别为textindex: true,如下所示:

{
  "products2": {
    "mappings": {
      "product": {
        "properties": {
          "color": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

如您所见,它还将关键字字段创建为多字段。该关键字字段已索引但未像文本一样进行分析。如果要删除此默认行为。您可以在创建索引时使用以下配置:

PUT products
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "product": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword",
              "index": false
            }
          }
        }
      ]
    }
  }
}

完成此操作后,索引将如下所示:

{
  "products": {
    "mappings": {
      "product": {
        "dynamic_templates": [
          {
            "strings": {
              "match_mapping_type": "string",
              "mapping": {
                "type": "keyword",
                "index": false
              }
            }
          }
        ],
        "properties": {
          "color": {
            "type": "keyword",
              "index": false
          },
          "type": {
            "type": "keyword",
              "index": false
          }
        }
      }
    }
  }
}

注意:我不知道这种情况,但是您可以使用@Kamal提到的多字段功能。否则,您将无法搜索未分析的字段。另外,您可以使用dynamic_templates映射集来分析某些字段。

请查看文档以获取更多信息:

https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

此外,我在this文章中对行为进行了解释。抱歉,这是土耳其语。您可以根据需要使用Google翻译检查示例代码示例。

答案 1 :(得分:0)

我建议您在映射中使用multi-fields(如果您不创建映射(动态映射),这将是默认行为)

这样,您可以在需要时切换到传统搜索完全匹配搜索。

请注意,对于完全匹配,您需要具有keyword数据类型+ Term Query。我指定的链接中提供了示例示例。

希望有帮助!

相关问题