将Copy_To的Attribute / Fluent映射与Nest结合使用

时间:2015-11-22 04:23:02

标签: elasticsearch nest

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

是否可以使用基于属性的映射,然后使用流畅的映射来添加copy_to?如果是的话,有什么例子吗?我很难找到答案。

我要复制的字段在我的模型类中不存在。我只是想在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; }
}

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

1 个答案:

答案 0 :(得分:3)

以下内容应该这样做

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