gpt4 book ai didi

c# - 使用Nest创建自定义分析器(用于电子邮件地址)

转载 作者:行者123 更新时间:2023-12-02 23:00:41 26 4
gpt4 key购买 nike

我有一个带有UserID字段的类,该字段包含一个邮件地址,并且我希望能够在该字段上进行搜索,以找到完全匹配的内容,或包含某些排除条件的邮件地址的一部分。

例如,如果UserID包含“john.doe@foo.com”,我希望能够搜索“john.doe@foo.com”,“john”和“doe”,但可以搜索“foo”和“com” ”应从索引中排除。

我尝试创建一个自定义分析器,该分析器使用带有终止词列表的停止过滤器,然后将多索引应用于该属性,如以下示例所示:

using Nest;
using System;
using System.Collections.Generic;

[ElasticType]
public class ElasticUser {
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string UserID { get; set; }
}

class Program {

static void Main(string[] args) {
const string IndexName = "test_index";

var settings = new ConnectionSettings(uri: new Uri("http://localhost:9200/"), defaultIndex: IndexName);
var client = new ElasticClient(settings);

// delete the index for the test
var deleteIndexResp = client.DeleteIndex(IndexName);

// create the custom filter and analyzer
var user_id_stop_filter = new StopTokenFilter {
Stopwords = new[] { "foo", "bar", "com" }
};
var user_id_analyzer = new CustomAnalyzer {
Filter = new List<string> {
"user_id_stop_filter"
},
Tokenizer = "letter"
};

// create the index with the custom filter and analyzer
var createIndexResp = client.CreateIndex(IndexName, index => index
.Analysis(analysis => analysis
.TokenFilters(t => t
.Add("user_id_stop_filter", user_id_stop_filter))
.Analyzers(a => a
.Add("user_id_analyzer", user_id_analyzer))));

// add a mapping for the "ElasticUser" type
var putMapResp = client.Map<ElasticUser>(
m => m.MapFromAttributes()
.Properties(properties => properties
.MultiField(multi => multi
.Name(p => p.UserID)
.Fields(fields => fields
.String(s => s
.Name(p => p.UserID)
.Index(FieldIndexOption.NotAnalyzed)
)
.String(s => s
.Name(p => p.UserID.Suffix("searchable"))
.Analyzer("user_id_analyzer")
)
)
)
));

// add a couple of entries
client.Index(new ElasticUser {
UserID = "some.one@foo.com"
});
client.Index(new ElasticUser {
UserID = "another.one@bar.com"
});
}

}

但是,这似乎不起作用,因为我只能搜索完全匹配的内容,但是电子邮件地址未标记化为非单词拆分。

如何使该多索引按说明工作?

当我尝试运行此查询时,没有得到任何结果:
GET /test_index/elasticuser/_search
{
"query": {
"query_string": {
"query": "one"
}
}
}

最佳答案

实现所需目标的最简单方法是在simple字段上简单地使用 searchable analyzer:

...
.String(s => s
.Name(p => p.UserID.Suffix("searchable"))
.Analyzer("simple") <---- change this
)
...

该电子邮件将被标记为任何非字母字符,并且您将能够搜索 johndoe

更新

如果您想保留排除列表,则绝对可以这样做。您可以保留现有的分析器,但需要使用 lowercase tokenizer(即与 simple分析器使用的相同)代替 letter
    var user_id_analyzer = new CustomAnalyzer {
Filter = new List<string> {
"user_id_stop_filter"
},
Tokenizer = "lowercase" <--- change this
};

更新2

在纯JSON中,这就是我所拥有的
curl -XPUT localhost:9200/users -d '{
"settings": {
"analysis": {
"analyzer": {
"email_analyzer": {
"type": "custom",
"tokenizer": "lowercase",
"filter": [
"my_stop"
]
}
},
"filter": {
"my_stop": {
"type": "stop",
"stopwords": [
"foo",
"bar",
"com"
]
}
}
}
},
"mappings": {
"user": {
"properties": {
"email": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
},
"parts": {
"type": "string",
"analyzer": "email_analyzer"
}
}
}
}
}
}
}'

然后,当我分析 some.one@foo.com时,这就是我得到的
$ curl -XGET 'localhost:9200/users/_analyze?field=email.parts&pretty' -d 'some.one@foo.com'
{
"tokens" : [ {
"token" : "some",
"start_offset" : 0,
"end_offset" : 4,
"type" : "word",
"position" : 1
}, {
"token" : "one",
"start_offset" : 5,
"end_offset" : 8,
"type" : "word",
"position" : 2
} ]
}

关于c# - 使用Nest创建自定义分析器(用于电子邮件地址),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34925297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com