我有一個帶有UserID
字段的類,其中包含郵件地址,我希望能夠在該字段中搜索完全匹配或部分郵件地址有一些排除。使用Nest創建自定義分析器(用於電子郵件地址)
例如,如果UserID
包含「[email protected]」,我希望能夠搜索「[email protected]」,「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 = "[email protected]"
});
client.Index(new ElasticUser {
UserID = "[email protected]"
});
}
}
然而,這並似乎不起作用,因爲我只能搜索完全匹配,但電子郵件地址不會被標記爲非字詞分裂。
我怎樣才能得到這個多指標工作描述?
當我嘗試運行此查詢,我沒有得到任何結果:
GET /test_index/elasticuser/_search
{
"query": {
"query_string": {
"query": "one"
}
}
}
什麼版本您使用的是Elasticsearch和NEST/Elasticsearch.NET的哪個版本? –