匹配特定字段但不匹配_all(ES 1.7)

时间:2016-06-20 16:19:45

标签: elasticsearch

当我运行此搜索时,我会找回我期待的记录:

POST users/user/_search
{"query":{"match":{"name":"smi"}}}

收益率:

{
  "took": 3,
  "timed_out": false,
  "_shards": { "total": 5, "successful": 5, "failed": 0 },
  "hits": {
    "total": 1,
    "max_score": 0.8838835,
    "hits": [
      {
        "_index": "users",
        "_type": "user",
        "_id": "648398826796745835",
        "_score": 0.8838835,
        "_source": {
          "email": "jonsmith@j.smith.enterprises",
          "name": "Jon Smith",
          "username": "jonsmith",
          "first_name": "Jon",
          "last_name": "Smith",
          "image_url": "blahblahblah"
        }
      }
    ]
  }
}

然而,当我进行类似的搜索时,我期望也可以工作,我什么也得不到:

POST users/user/_search
{"query":{"match":{"_all":"smi"}}}

收率:

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

映射非常简单:

GET users/_mapping/user

{
  "users": {
    "mappings": {
      "user": {
        "properties": {
          "email": { "type": "string", "analyzer": "our_email" },
          "email_username": { "type": "string", "analyzer": "email_username" },
          "first_name": { "type": "string", "analyzer": "our_name" },
          "image_url": { "type": "string", "index": "not_analyzed" },
          "last_name": { "type": "string", "analyzer": "our_name" },
          "name": { "type": "string", "analyzer": "our_name" },
          "username": { "type": "string", "analyzer": "our_name" }
        }
      }
    }
  }
}

our_name分析器是这样实现的,虽然我无法在1.7 API中找到在分析器配置上进行GET的方法:

{
  "analysis": {
    "analyzer": {
      "our_name": {
        "filter": [
          "lowercase",
          "word_delimiter",
          "our_edgeNgram25"
        ],
        "tokenizer": "standard"
      }
    },
    "filter": {
      "our_edgeNgram25": { "max_gram": 5, "min_gram": 2, "type": "edgeNGram" },
      "our_email_filter": {
        "patterns": [ "(.+)@(.+)" ],
        "type": "pattern_capture"
      }
    }
  }
}

在user.name字段上运行演示分析让我认为它应该按原样匹配:

GET users/_analyze?analyzer=our_name&text=jon%20smith
{
  "tokens": [
    {
      "token": "jo",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 1
    },
    {
      "token": "jon",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 1
    },
    {
      "token": "sm",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 2
    },
    {
      "token": "smi",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 2
    },
    {
      "token": "smit",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 2
    },
    {
      "token": "smith",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 2
    }
  ]
}

建议表示赞赏,谢谢。

1 个答案:

答案 0 :(得分:1)

_all默认使用标准分析器,因此您无法找到子字符串,在映射中为_all指定分析器