简体   繁体   English

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

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

I use Searchblox to index and search my files, which itself calls ES 2.x to do the job. 我使用Searchblox索引和搜索我的文件,该文件本身称为ES 2.x以完成工作。 Searchblox uses a "mapping.json" file to initialize a mapping upon the creation of an index. Searchblox使用“ mapping.json”文件在创建索引时初始化映射。 Here's the link to that file. 这是该文件的链接 As "@Russ Cam" suggested here , I created my own class content with the following code (just like he did with the "questions" index and "Question" class): 正如“ @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

These fields from the content class (type,store etc.) come from the mapping.json file attached above. 内容类中的这些字段(类型,存储等)来自上面所附的mapping.json文件。 Now, when I (just like you showed me) execute the following code: 现在,当我(就像您向我展示的那样)执行以下代码:

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

All I get as a response on the searchResponse variable is: 作为对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)

In brief, all I want is to be able to : 简而言之,我想要做的就是:

  1. get all the documents (no criteria) 获取所有文件(无条件)
  2. get all the documents within a time range and based upon keywords.. such as "service" 获取一个时间范围内并基于关键字的所有文档..例如“服务”

What am I doing wrong? 我究竟做错了什么? I can provide with more information if needed.. Thank you all for your detailed answers. 如果需要,我可以提供更多信息。。谢谢大家的详细回答。

Your C# POCO is not correct in regards to your mapping; 您的C#POCO就您的映射而言是不正确的; your document type is "sdoc" and each of the properties under the "properties" property is a field on that document type; 您的文档类型是"sdoc""properties"属性下的每个属性都是该文档类型上的一个字段; These fields map to properties on your C# POCO. 这些字段映射到C#POCO上的属性。

As an example to get you started 以入门为例

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

    public string Content { get; set; }
}

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

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

We set up the client with some inference rules for the Document type which will be used when interacting with Elasticsearch. 我们为客户设置了一些Document类型的推理规则,这些规则将在与Elasticsearch交互时使用。 The above query emits the following query json 上面的查询发出以下查询json

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

As an aside, I noticed that the mapping contained a multi_field type; multi_field说一句,我注意到映射包含一个multi_field类型。 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. 在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