且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法从弹性搜索中获取NEST的任何文档

更新时间:2023-02-18 23:28:03

你的C#POCO是不正确的关于你的映射;您的文档类型为sdoc属性属性下的每个属性都是该文档上的一个字段类型;这些字段映射到您的C#POCO上的属性。

Your C# POCO is not correct in regards to your mapping; your document type is "sdoc" and each of the properties under the "properties" property is a field on that document type; These fields map to properties on your C# POCO.

作为一个例子让你开始

public class Document
{
    [String(Name = "uid")]
    public string UId { get; set; }

    public string Content { get; set; }
}

默认情况下,NEST将骆驼案件POCO属性名称,因此内容将根据您的映射情况正确,但是,为了命名,我们使用uid字段的属性映射它匹配映射(我们可以在这里进一步设置附加属性属性值以完全匹配映射; 请参阅automapping文档)。

NEST by default will camel case POCO property names, so "content" will be case correctly according to your mapping, however, we use attribute mapping for the "uid" field in order to name it to match the mapping (we can go further here and set additional attribute property values to fully match the mapping; see the automapping documentation).

现在,要搜索文档,让我们创建连接设置和客户端使用

Now, to search with the document, let's create the connection settings and a client to use

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var connectionSettings = new ConnectionSettings(pool)
            .InferMappingFor<Document>(t => t
                // change the index name to the name of your index :)
                .IndexName("index-name")
                .TypeName("sdoc")
                .IdProperty(p => p.UId)
            );

    var client = new ElasticClient(connectionSettings);

    // do something with the response
    var searchResponse = client.Search<Document>(s => s
        .Query(q => q
            .Match(m => m
                .Field(f => f.Content)
                .Query("service")
            )
        )
    );
}

我们为文档与弹性搜索交互时将使用的类型。以上查询发出以下查询json

We set up the client with some inference rules for the Document type which will be used when interacting with Elasticsearch. The above query emits the following query json

{
  "query": {
    "match": {
      "content": {
        "query": "service"
      }
    }
  }
}

除此之外,我注意到映射包含一个 multi_field 类型; multi_field 类型在Elasticsearch 1.0中被删除(多个字段仍然存在,实际类型不存在),因此请确保您实际上在Searchblox上运行Elasticsearch 2.x,因为仅支持NEST 2.x Elasticsearch 2.x。

As an aside, I noticed that the mapping contained a multi_field type; multi_field types were removed in Elasticsearch 1.0 (multi fields are still there, just the actual type is not), so be sure that you're actually running Elasticsearch 2.x on Searchblox, as NEST 2.x is only supported against Elasticsearch 2.x.