无法在elasticsearch 5.4中搜索短语

时间:2017-07-20 05:33:14

标签: elasticsearch

我正在搜索电子邮件正文中的短语。需要获取过滤的确切数据,如果我搜索“新建道路”,它应该仅返回包含“新建道路”字样的结果。不是' Avenue Street',' Park Avenue'等

我的映射就像:

{
  "exchangemailssql": {
  "aliases": {},
  "mappings": {
     "email": {
        "dynamic_templates": [
           {
              "_default": {
                 "match": "*",
                 "match_mapping_type": "string",
                 "mapping": {
                    "doc_values": true,
                    "type": "keyword"
                 }
              }
           }
        ],
        "properties": {
           "attachments": {
              "type": "text",
              "fields": {
                 "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                 }
              }
           },
           "body": {
              "type": "text",
              "analyzer": "keylower",
              "fielddata": true
           },

           "count": {
              "type": "short"
           },
           "emailId": {
              "type": "long"
           }              
        }
     }
  },
  "settings": {
     "index": {
        "refresh_interval": "3s",
        "number_of_shards": "1",
        "provided_name": "exchangemailssql",
        "creation_date": "1500527793230",
        "analysis": {
           "filter": {
              "nGram": {
                 "min_gram": "4",
                 "side": "front",
                 "type": "edge_ngram",
                 "max_gram": "100"
              }
           },
           "analyzer": {
              "keylower": {
                 "filter": [
                    "lowercase"
                 ],
                 "type": "custom",
                 "tokenizer": "keyword"
              },
              "email": {
                 "filter": [
                    "lowercase",
                    "unique",
                    "nGram"
                 ],
                 "type": "custom",
                 "tokenizer": "uax_url_email"
              },
              "full": {
                 "filter": [
                    "lowercase",
                    "snowball",
                    "nGram"
                 ],
                 "type": "custom",
                 "tokenizer": "standard"
              }
           }
        },
        "number_of_replicas": "0",
        "uuid": "2XTpHmwaQF65PNkCQCmcVQ",
        "version": {
           "created": "5040099"
        }
     }
  }
 }
}

我已经给出了搜索查询:

{
   "query": {
  "match_phrase": {
     "body": "Avenue New"
  }
   },
    "highlight": {
    "fields" : {
        "body" : {}
    }
}
}

1 个答案:

答案 0 :(得分:2)

这里的问题是您使用关键字标记器对整个正文内容进行标记,即它将是一个大的小写字符串,您无法在其中搜索。

如果您只是将body字段的分析器更改为standard而不是keylower,则可以使用match_phrase查询找到所需内容。< / p>

       "body": {
          "type": "text",
          "analyzer": "standard",   <---change this
          "fielddata": true
       },