无法在ElasticSearch中获取正确索引的电子邮件字段

时间:2015-01-17 16:49:54

标签: mongodb elasticsearch mongoose elasticsearch-plugin

我正在尝试设置一个电子邮件字段,以便在我的映射中正确编制索引。

因为我不想在索引时对电子邮件进行标记化,所以我之前已经指定了以下映射,以便在整个字符串匹配时仅匹配搜索。

{
  "users": {
    "mappings": {
      "user": {
        "properties": {
          "email": {
            "type": "string",
            "index": "not_analyzed"
          },
          "name": {
            "type": "string",
            "fields": {
              "raw": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "nickname": {
            "type": "string"
          }
        }
      }
    }
  }
}

除非我希望将电子邮件昵称字段作为小写字母进行比较,否则这样做有效。

我尝试了几种方法来指定更改映射以使用小写标记过滤器。

我通过以下方式完成了这项工作:

{
  "settings":{
    "index":{
      "analysis":{
        "analyzer":{
          "lowercase_analyzer":{
            "tokenizer":"standard", //Also tried 'Simple' and 'Keyword'
            "filter":"lowercase"
          }
        }
      }
    }
  },

  "mappings": {
    "user": {
      "properties": {
        "email": {
          "type": "string",
          "analyzer":"lowercase_analyzer",
          "index": "not_analyzed" //Tried with and without this
        },
        "name": {
          "type": "string",
          "analyzer":"lowercase_analyzer",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "nickname": {
          "type": "string",
          "analyzer": "lowercase_analyzer"
        }
      }
    }
  }
}

我希望允许以下行为:

  • 可以搜索电子邮件并将其作为小写进行比较,并且只有在整个电子邮件匹配时才匹配
  • name是一个用于搜索和排序的多字段,使用小写
  • 昵称比较小写

1 个答案:

答案 0 :(得分:3)

您可以尝试以下操作,看看是否符合您的要求 -      {

  "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "flat":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
     }
    },

    "mappings": {
     "user": {
        "properties": {
           "email": {
             "type": "string",
             "analyzer":"flat"
           },
           "name": {
              "type": "string",
               "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
           },
           "nickname": {
              "type": "string"
           }
        }
     }
  }
}