1

我正在尝试使用 NEST c# 客户端在 ElasticSearch 中进行一个没有重音的查询,我的数据有带有重音的葡萄牙语拉丁词。见下面的代码:

var result = client.Search<Book>(s => s
    .From(0)
    .Size(20)
    .Fields(f => f.Title)
    .FacetTerm(f => f.OnField(of => of.Genre))
    .Query(q => q.QueryString(qs => qs.Query("sao")))
);

这次搜索没有找到任何东西。我在该索引上的数据包含许多标题,例如:“ São Cristóvan”、“ São Gonçalo”。

var settings = new IndexSettings();
settings.NumberOfReplicas = 1;
settings.NumberOfShards = 5;
settings.Analysis.Analyzers.Add("snowball", new Nest.SnowballAnalyzer { Language = "Portuguese" });
var idx5 = client.CreateIndex("idx5", settings);

如何使用 ElasticSearch查询“ sao ”并找到“ são ”?

我认为必须创建具有正确属性的索引,但我已经尝试了许多设置,例如。

或在原始模式下:

    {
     “idx”:{
       “设置”:{
         “index.analysis.filter.jus_stemmer.name”:“巴西人”,
         “index.analysis.filter.jus_stop._lang_”:“巴西”
       }
     }
    }

如何进行搜索并忽略重音符号?

谢谢朋友,

4

3 回答 3

4

查看解决方案:

使用 putty 执行连接弹性搜索搜索:

curl -XPOST 'localhost:9200/idx30/_close'

curl -XPUT 'localhost:9200/idx30/_settings' -d '{
            "index.analysis.analyzer.default.filter.0": "standard",
            "index.analysis.analyzer.default.tokenizer": "standard",
            "index.analysis.analyzer.default.filter.1": "lowercase",
            "index.analysis.analyzer.default.filter.2": "stop",
            "index.analysis.analyzer.default.filter.3": "asciifolding",
            "index.number_of_replicas": "1"
}'

curl -XPOST 'localhost:9200/idx30/_open'

将“ idx30 ”替换为您的索引名称

完毕!

于 2013-05-03T12:20:28.173 回答
3

我偶然发现了这个线程,因为我遇到了同样的问题。这是使用 AsciiFolding Analyzer 创建索引的 NEST 代码:

// Create the Client
string indexName = "testindex";
var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).SetDefaultIndex(indexName);
var client = new ElasticClient(settings);
// Create new Index Settings
IndexSettings set = new IndexSettings();
// Create a Custom Analyzer ...
var an = new CustomAnalyzer();
// ... based on the standard Tokenizer
an.Tokenizer = "standard";
// ... with Filters from the StandardAnalyzer
an.Filter = new List<string>();
an.Filter.Add("standard");
an.Filter.Add("lowercase");
an.Filter.Add("stop");
// ... just adding the additional AsciiFoldingFilter at the end
an.Filter.Add("asciifolding");
// Add the Analyzer with a name
set.Analysis.Analyzers.Add("nospecialchars", an);
// Create the Index
client.CreateIndex(indexName, set);

现在您可以将您的实体映射到此索引(在创建索引后执行此操作很重要)

client.MapFromAttributes<TestEntity>();

下面是这样一个实体的样子:

[ElasticType(Name = "TestEntity", DisableAllField = true)]
public class TestEntity
{
    public TestEntity(int id, string desc)
    {
        ID = id;
        Description = desc;
    }

    public int ID { get; set; }

    [ElasticProperty(Analyzer = "nospecialchars")]
    public string Description { get; set; }
}

好了,描述字段现在插入到索引中,没有重音符号。如果您检查索引的映射,您可以对此进行测试:

http://localhost:9200/testindex/_mapping

然后应该看起来像:

{
    testindex: {
        TestEntity: {
            _all: {
                enabled: false
            },
            properties: {
                description: {
                    type: "string",
                    analyzer: "nospecialchars"
                },
                iD: {
                    type: "integer"
                }
            }
        }
    }
}

希望这会对某人有所帮助。

于 2013-06-28T16:09:03.090 回答
0

您需要在分析仪中加入ACSII 折叠过滤器来完成此操作。这将意味着构建雪球分析器表单标记器和过滤器(除非nest允许您将过滤器添加到非自定义分析器。但据我所知,ElasticSearch 没有)。

SnowballAnalyzer 包含:

  • 标准分词器
  • 标准过滤器
  • (在此处添加 ASCIIFolding 过滤器)
  • 小写过滤器
  • StopFilter(设置了适当的停用词)
  • SnowballFilter(使用适当的语言)
  • (或者也许在这里)

我可能会尝试ASCIIFoldingFilter在 LowercaseFilter 之前添加,尽管最好将其添加为最后一步(在 SnowballFilter 之后)。两种方式都试一下,看看哪个效果更好。我对 Protuguese 词干分析器的了解还不够,无法确定哪个是最好的。

于 2013-05-02T16:26:47.527 回答