【问题标题】:Elasticsearch - Nest - Missing First CharacterElasticsearch - 嵌套 - 缺少第一个字符
【发布时间】:2014-07-20 20:45:51
【问题描述】:

我正在针对 Elasticsearch 使用 Nest 客户端。我正在使用 n-gram 索引分析器。我注意到一些奇怪的行为 - 当我从头开始搜索单词时,我没有得到任何结果。但是,如果我从第二个字符开始搜索,它会完美运行。这些只是普通的英文字母。

因此,例如,如果我搜索“itty”、“itt”、“tty”等,它会找到包含“kitty”的词,而不是“ki”、“kit”等。这几乎就像 n -gram 只是跳过第一个字符。

我不确定这是否是由 Nest 引起的,或者这是否是 n-gram 的正常行为。我的索引设置看起来类似于这篇文章中的设置:Elasticsearch using NEST: How to configure analyzers to find partial words?,除了我的 max-gram 只有 10。

更新

我稍微简化了我的代码并验证了相同的行为。

这是使用 Nest 定义的映射配置:

const string index = "myApp";
const string type = "account";
const string indexAnalyzer = "custom_ngram_analyser";
const string searchAnalyzer = "standard";
const string tokenizer = "custom_ngram_tokenizer";
const string tokenFilter = "custom_ngram_tokenFilter";
...
client.CreateIndex(index, i => i
        .Analysis(ad => ad
            .Analyzers(a => a.Add(indexAnalyzer, new CustomAnalyzer() { Tokenizer = tokenizer }))
            .Tokenizers(t => t.Add(tokenizer, new NGramTokenizer() { MinGram = 1, MaxGram = 15 }))
            .TokenFilters(f => f.Add(tokenFilter, new NgramTokenFilter() { MinGram = 1, MaxGram = 15 })))
        .TypeName(account);
        .IdField(r => r.SetPath("accountId").SetIndex("not_analyzed").SetStored(true));
        .Properties(ps => ps.Number(p => p.Name(r => r.AccountId)
                                          .Index(NonStringIndexOption.not_analyzed)
                                          .Store(true));
                            .String(p => p.Name(r => r.AccountName)
                                          .Index(FieldIndexOption.analyzed)
                                          .IndexAnalyzer(indexAnalyzer)
                                          .SearchAnalyzer(searchAnalyzer)
                                          .Store(true)
                                          .TermVector(TermVectorOption.no))));

这是缺少第一个字符的搜索:

SearchCriteria criteria = new SearchCriteria() { AccountName = "kitty" };

client.Search<SearchAccountResult>(s => s
    .Index(index)
    .Type(type)
    .Query(q => q.Bool(b => b.Must(d => d.Match(m => m.OnField(r => r.AccountName).QueryString(criteria.AccountName)))))
    .SortDescending("_score"))

【问题讨论】:

  • 嘿 Travis Parks,您介意发布您的地图和搜索查询吗?
  • @Greg Marzouka 我更新了我的问题。
  • 嗯,对我来说似乎工作正常。澄清一下,上面itty 的相同代码会返回结果吗?您可以通过执行 GET /myapp/_mapping 从 ES 发布您的映射吗?另外,如果您执行 GET /myapp/_analyze?analyzer=custom_ngram_analysisr&text=kitty,kitty 是一个令牌吗?另一件事-我假设这只是您的示例,但 myApp 不是小写字母,ES 在创建索引时会拒绝。
  • Itty 正在返回结果。
  • 嘿@Travis Parks,很高兴你发现了这个问题。仅供参考 - 在发布 refresh 之前,索引后文档不可用于搜索。这可以设置为以特定间隔在索引上发生(默认为每 1 秒),但您也可以手动发出刷新,但对性能有一些轻微影响。但是,您可以在使用 get API 建立索引后立即检索文档。

标签: elasticsearch nest n-gram text-analysis


【解决方案1】:

我遇到了这个问题,因为最初我的索引是区分大小写的。我所有的测试数据都以大写字母开头。

我将其更改为不区分大小写,但更新并未立即进行。尽管分析器似乎配置为不区分大小写,但索引并未刷新。

清除索引并从头开始重新填充它解决了这个问题。

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 2020-05-07
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2022-11-05
    • 2020-03-20
    • 2019-12-23
    相关资源
    最近更新 更多