繁体   English   中英

无法从Elasticsearch获取带有NEST的任何文档

[英]Can't get any documents with NEST from elasticsearch

我使用Searchblox索引和搜索我的文件,该文件本身称为ES 2.x以完成工作。 Searchblox使用“ mapping.json”文件在创建索引时初始化映射。 这是该文件的链接 正如“ @Russ Cam” 在这里建议的那样,我使用以下代码创建了自己的课程内容(就像他对“ questions”索引和“ Question”课程所做的一样):

public class Content
{
    public string type { get; set; }
    public Fields fields { get; set; }
}

public class Fields
{
    public Content1 content { get; set; }
    public Autocomplete autocomplete { get; set; }
}

public class Content1
{
    public string type { get; set; }
    public string store { get; set; }
    public string index { get; set; }
    public string analyzer { get; set; }
    public string include_in_all { get; set; }
    public string boost { get; set; }
} //got this with paste special->json class

内容类中的这些字段(类型,存储等)来自上面所附的mapping.json文件。 现在,当我(就像您向我展示的那样)执行以下代码:

var searchResponse = highLevelclient.Search<Content>(s => s.Query(q => q
         .Match(m => m.Field(f => f.fields.content)
        .Query("service")

作为对searchResponse变量的响应,我得到的是:

Valid NEST response built from a successful low level call on POST: /idx014/content/_search
Audit trail of this API call:
 -HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.7180404
Request:
{"query":{"match":{"fields.content":{"query":"service"}}}}
Response:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
And no documents in searchResponse.Documents. Contradictorily, when I search for the "service" query on Searchblox or make an API call to localhost:9200 with the Sense extension of Google Chrome, I get 2 documents. (the documents that I was looking for)

简而言之,我想要做的就是:

  1. 获取所有文件(无条件)
  2. 获取一个时间范围内并基于关键字的所有文档..例如“服务”

我究竟做错了什么? 如果需要,我可以提供更多信息。。谢谢大家的详细回答。

您的C#POCO就您的映射而言是不正确的; 您的文档类型是"sdoc""properties"属性下的每个属性都是该文档类型上的一个字段; 这些字段映射到C#POCO上的属性。

以入门为例

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

    public string Content { get; set; }
}

默认情况下,NEST将使用驼峰式POCO属性名称,因此根据您的映射, "content"将正确区分大小写,但是,我们对"uid"字段使用属性映射,以便对其进行命名以匹配该映射(我们可以在此处进一步介绍)并设置其他属性属性值以完全匹配映射; 请参阅自动映射文档 )。

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

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")
            )
        )
    );
}

我们为客户设置了一些Document类型的推理规则,这些规则将在与Elasticsearch交互时使用。 上面的查询发出以下查询json

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

multi_field说一句,我注意到映射包含一个multi_field类型。 在Elasticsearch 1.0中删除了multi_field类型 (仍然存在多个字段,但没有实际的类型),因此请确保您实际上在Searchblox上运行Elasticsearch 2.x,因为仅Elasticsearch 2支持NEST2.x。 X。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM