ElasticSearch edgeNGram

时间:2014-01-03 04:00:44

标签: elasticsearch

我有以下设置和分析器:

put /tests
{
"settings": {
    "analysis": {
         "analyzer": {
             "standardWithEdgeNGram": {
                 "tokenizer": "standard",
                 "filter": ["lowercase", "edgeNGram"]
             }
         },
         "tokenizer": {
             "standard": {
                 "type": "standard"
             }
         },
         "filter": {
             "lowercase": {
                "type": "lowercase"
            },
            "edgeNGram": {
                "type": "edgeNGram",
                "min_gram": 2,
                "max_gram": 15,
                "token_chars": ["letter", "digit"]
            }
        }
    }
},
"mappings": {
    "test": {
        "_all": {
            "analyzer": "standardWithEdgeNGram"
        },
        "properties": {
            "Name": {
                "type": "string",
                "analyzer": "standardWithEdgeNGram"
            }
        }
   }
}
}

我将以下数据发布到其中:

POST /tests/test
{
    "Name": "JACKSON v. FRENKEL"
}

这是我的疑问:

GET /tests/test/_search
{
    "query": {
        "match": {
           "Name": "jax"
        }
    }
}

我得到了这个结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
},
"hits": {
    "total": 1,
    "max_score": 0.19178301,
    "hits": [
        {
            "_index": "tests",
            "_type": "test",
            "_id": "lfOxb_5bS86_CMumo_ZLoA",
            "_score": 0.19178301,
            "_source": {
                "Name": "JACKSON v. FRENKEL"
            }
        }
    ]
}
}

有人可以向我解释“名字”中没有“jax”,它仍然可以得到匹配吗?

提前致谢

2 个答案:

答案 0 :(得分:4)

match查询对其给定值执行分析。默认情况下,正在使用"jax"分析standardWithEdgeNGram,其中包括将其置于["ja", "ax"]的n-gram分析,其中第一个与分析的"ja"匹配"JACKSON v. FRENKEL" }}

如果您不想要此行为,可以使用match字段指定与analyzer不同的分析器,例如keyword

GET /tests/test/_search
{
    "query": {
        "match": {
           "Name": "jax",
           "analyzer" : "keyword"
        }
    }
}

答案 1 :(得分:1)

在ES 1.3.2中,以下查询给出了错误

GET /tests/test/_search
{
    "query": {
    "match": {
       "Name": "jax",
       "analyzer" : "keyword"
       }
  }
}

错误:查询以简化形式解析,带有直接字段名称,但包含的选项多于字段名称,可能使用“选项”形式,带有“查询”元素?]; }] 状态:400

我将问题修复为below

{
   "query": {
       "query_string": {
            "fields": [
            "Name"
             ],
        "query": "jax",
            "analyzer": "simple"
        }
    }
}