我有一个名为“IndexModel”的类:
public class IndexModel
{
[ElasticProperty(Index= FieldIndexOption.NotAnalyzed, Store = true)]
public string ModelNumber{ get; set; }
}
以下是我如何设置弹性客户端:
var uri = new Uri("http://localhost:9200");
var config = new ConnectionSettings(uri);
var client = new ElasticClient(config);
client.Map<IndexModel>(m => m.MapFromAttributes());
我可以看到响应的映射结果:
Request {
"indexmodel": {
"properties": {
"modelNumber": {
"type": "string",
"store": true,
"index": "not_analyzed"
},
}
}
}
我有一个这种类型的索引记录,“ModelNumber”属性的值是“test-123”,以下是我的查询:
var result = client.Search<IndexModel>(s => s.Query(new TermQuery() { Field = Property.Path<IndexModel>(it => it.ModelNumber), Value = "test-123"}));
这是我得到的最终映射请求:
Method: POST,
Url: http://localhost:9200/_search,
Request: {
"query": {
"term": {
"modelNumber": {
"value": "test-123"
}
}
}
}
但是我无法得到结果,如果我将“ModelNumber”属性的值更改为“test123”,重新索引它,并通过关键字“test123”进行搜索,那么它是可行的,所以我认为分析仪仍然分析了“ModelNumber”属性,有人可以帮助我,谢谢。
答案 0 :(得分:5)
我遇到了同样的问题,解决方法是首先创建索引,然后放置映射,最后添加数据。
将“类型属性”添加到模型字段
[ElasticProperty(OmitNorms = true, Index = FieldIndexOption.NotAnalyzed)]
var node = new Uri("http://192.168.0.56:9200/");
var settings = new ConnectionSettings(node, defaultIndex: "ticket");
var client = new ElasticClient(settings);
var createIndexResult = client.CreateIndex("ticket");
var mapResult = client.Map<TicketElastic>(c => c.MapFromAttributes().IgnoreConflicts().Type("TicketElastic").Indices("ticket"));