elasticsearch NEST 客户端,当搜索关键字包含连字符时,仍会分析属性“not_analyzed”

2024-03-01

我有一个名为“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”属性,有人可以帮助我吗,谢谢。


我遇到了同样的问题,解决方案是首先创建索引,然后放置映射,最后添加数据。

将类型属性添加到模型字段

 [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"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

elasticsearch NEST 客户端,当搜索关键字包含连字符时,仍会分析属性“not_analyzed” 的相关文章

随机推荐