【问题标题】:ElasticSearch edgeNGramElasticSearch edgeNgram
【发布时间】:2014-11-03 16:15:27
【问题描述】:

我有以下设置和分析器:

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”,并且仍然可以匹配吗?

提前致谢

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    如果您不希望这种行为,您可以使用analyzer 字段为match 指定不同的分析器,例如keyword

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

    【讨论】:

    • 非常感谢,保罗。 Solr 和 ES 似乎分享了一些想法,他们都有索引分析器和查询分析器。
    • Solr 和 ES 都使用 Lucene 引擎,所以自然而然地共享了很多特性!
    【解决方案2】:

    在 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"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多