将 Copy_To 的属性/流畅映射与 Nest 相结合

Combining Attribute / Fluent Mapping for Copy_To with Nest

我想在 Nest 中使用 copy_to 功能。我读到我需要使用流畅的映射 (Elasticsearch Nest and CopyTo)。

是否可以使用基于属性的映射,然后在其之上使用流畅的映射来添加 copy_to?如果是这样,有没有例子?我很难找到答案。

我要复制到的字段在我的模型中不存在 class。我只想在elasticsearch中搜索它。

[ElasticType(IdProperty = "CustomerId", Name = "customer_search")]
public class CustomerSearchResult : BindableBase
{
    [ElasticProperty(Name = "customer_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public int CustomerId { get; set; }
    [ElasticProperty(Name = "account_type", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string AccountType { get; set; }
    [ElasticProperty(Name = "short_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string ShortName { get; set; }
    [ElasticProperty(Name = "legacy_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string LegacyName { get; set; }
    [ElasticProperty(Name = "legacy_contact_name", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string LegacyContactName { get; set; }
    [ElasticProperty(Name = "city", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string City { get; set; }
    [ElasticProperty(Name = "state_abbreviation", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string StateAbbreviation { get; set; }
    [ElasticProperty(Name = "country", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string Country { get; set; }
    [ElasticProperty(Name = "postal_code", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
    public string PostalCode { get; set; }
}

在上面的 class 中,我想使用 ShortName、LegacyName 和 LegacyContactName 以及 copy_to 一个名为 "search" 的字段,这将是一个分析字段。

像下面这样的事情应该做

void Main()
{
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
    var connection = new InMemoryConnection(settings);
    var client = new ElasticClient(connection: connection);


    var indexResponse = client.CreateIndex("customer_searches", c => c
        .AddMapping<CustomerSearchResult>(m => m
            .MapFromAttributes()
            .Properties(p => p
                .String(s => s.Name("short_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
                .String(s => s.Name("legacy_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
                .String(s => s.Name("legacy_contact_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
                .String(s => s.Name("search").Index(FieldIndexOption.Analyzed))
            )
        )
    );

    Console.WriteLine(Encoding.UTF8.GetString(indexResponse.RequestInformation.Request));
}

输出

{
  "settings": {
    "index": {}
  },
  "mappings": {
    "customer_search": {
      "properties": {
        "customer_id": {
          "index": "not_analyzed",
          "type": "string"
        },
        "account_type": {
          "index": "not_analyzed",
          "type": "string"
        },
        "short_name": {
          "index": "not_analyzed",
          "copy_to": [
            "search"
          ],
          "type": "string"
        },
        "legacy_name": {
          "index": "not_analyzed",
          "copy_to": [
            "search"
          ],
          "type": "string"
        },
        "legacy_contact_name": {
          "index": "not_analyzed",
          "copy_to": [
            "search"
          ],
          "type": "string"
        },
        "city": {
          "index": "not_analyzed",
          "type": "string"
        },
        "state_abbreviation": {
          "index": "not_analyzed",
          "type": "string"
        },
        "country": {
          "index": "not_analyzed",
          "type": "string"
        },
        "postal_code": {
          "index": "not_analyzed",
          "type": "string"
        },
        "search": {
          "index": "analyzed",
          "type": "string"
        }
      }
    }
  }
}

Properties() 的调用会覆盖默认约定和属性映射,因此您需要指定字段 not_analyzed 也在那里。