Elasticsearch NEST V2完成上下文映射

时间:2016-05-12 14:28:10

标签: elasticsearch autocomplete nest

我有提供自动完成功能所需的功能,只能在索引中返回特定类型的文档。

我有自动完成建议工作没有应用上下文。但是当我尝试映射上下文时,它就失败了。

这是我的映射。

.Map<MyType>(l => l
.Properties(p => p
    .Boolean(b => b
        .Name(n => n.IsArchived)
    )
    .String(s => s
        .Name(n => n.Type)
        .Index(FieldIndexOption.No)
    )
    .AutoMap()
    .Completion(c => c
        .Name(n => n.Suggest)
        .Payloads(false)
        .Context(context => context
            .Category("type", cat => cat
                .Field(field => field.Type)
                .Default(new string[] { "defaultType" })
            )
        )
    )
)

由于intellisense或build中没有任何错误,因此不确定我做错了什么。

1 个答案:

答案 0 :(得分:1)

The Context Suggester mapping不正确,不会按原样编译; AutoMap()不是PropertiesDescriptor<T>上的方法,而是PutMappingDescriptor<T>上的方法。看看the completion suggester mapping that is used as part of the integration tests。它应该如下所示

public class MyType
{
    public bool IsArchived { get; set;}

    public string Type { get; set;}

    public  CompletionField<object> Suggest { get; set;}
}

client.Map<MyType>(l => l
    .AutoMap()
    .Properties(p => p
        .Boolean(b => b
            .Name(n => n.IsArchived)
        )
        .String(s => s
            .Name(n => n.Type)
            .Index(FieldIndexOption.No)
        )

        .Completion(c => c
            .Name(n => n.Suggest)
            .Context(context => context
                .Category("type", cat => cat
                    .Field(field => field.Type)
                    .Default("defaultType")
                )
            )
        )
    )
);

导致以下映射

{
  "properties": {
    "isArchived": {
      "type": "boolean"
    },
    "type": {
      "type": "string",
      "index": "no"
    },
    "suggest": {
      "type": "completion",
      "context": {
        "type": {
          "type": "category",
          "path": "type",
          "default": [
            "defaultType"
          ]
        }
      }
    }
  }
}