【问题标题】:ElasticSearch Issue With Matching Results匹配结果的 ElasticSearch 问题
【发布时间】:2016-03-24 15:09:50
【问题描述】:

我在查询“ford”是否在数据库中时遇到问题,我搜索“fordddddddd”它会返回匹配项。我有用于子部分匹配的 ngram,用于像 'fo'、for'、ford' 这样的查询,但 'fordddddd' 不应该匹配。可能是什么问题?以下是我的设置、映射和查询。

设置:

  settings: {
    number_of_shards: 1,
    analysis: {
      filter: {
        ngram_filter: {
          type: 'edge_ngram',
          min_gram: 2,
          max_gram: 15
        }
      },
      analyzer: {
        ngram_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: [
            'lowercase',
            'ngram_filter'
          ]
        }
      }
    }
  }

映射:

  mappings: [
    {
      name: 'car',
      _all: {
        type: 'string',
        analyzer: 'ngram_analyzer'
      },
      properties: {
        description: {
          properties: {
            name: {
              type: 'string',
              include_in_all: true,
              term_vector: 'yes',
              analyzer: 'ngram_analyzer'
            },
            model: {
              type: 'string',
              include_in_all: true,
              term_vector: 'yes',
              analyzer: 'ngram_analyzer'
            }
          }
        }        
      }
    }
  ]

查询:

GET car/_search
{
    "query": {
        "match": {
           "_all": {
               "query": "Forddddddd"
           }
        }
    }
}

【问题讨论】:

    标签: search elasticsearch match matching n-gram


    【解决方案1】:

    这里的问题是您对indexing 和searching 使用相同的analyzer。

    当您搜索Forddddd 时,应用ngram_analyzer 并生成令牌fo, for, ford 并因此为您提供结果,您应该指定standard analyzer 进行搜索,您将得到想要的结果。

    properties: {
        name: {
            type: 'string',
            include_in_all: true,
            term_vector: 'yes',
            analyzer: 'ngram_analyzer',
            search_analyzer: 'standard' <--- here
        },
        model: {
            type: 'string',
            include_in_all: true,
            term_vector: 'yes',
            analyzer: 'ngram_analyzer',
            search_analyzer: 'standard' <--- here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2016-09-14
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多