首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Copy_To与Nest的属性/ Fluent映射相结合

Copy_To与Nest的属性/ Fluent映射相结合
EN

Stack Overflow用户
提问于 2015-11-22 04:23:02
回答 1查看 1.3K关注 0票数 1

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

是否可以使用基于属性的映射,然后在此基础上使用fluent映射来添加copy_to?若然,是否有例子?我很难找到答案。

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

代码语言:javascript
运行
复制
[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 --一个名为“搜索”的字段,它将是一个分析字段。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-22 22:24:40

像下面这样的东西应该可以做到

代码语言:javascript
运行
复制
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));
}

哪种输出

代码语言:javascript
运行
复制
{
  "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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33851566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档