无法从弹性搜索中获取NEST的任何文档 [英] Can't get any documents with NEST from elasticsearch

查看:1063
本文介绍了无法从弹性搜索中获取NEST的任何文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Searchblox来索引和搜索我的文件,它本身称为ES 2.x来完成这项工作。 Searchblox使用mapping.json文件在创建索引时初始化映射。以下是该文件的链接。由于@Russ Cam建议此处,我使用以下代码创建了自己的课程内容(就像他在问题索引和问题类):

I use Searchblox to index and search my files, which itself calls ES 2.x to do the job. Searchblox uses a "mapping.json" file to initialize a mapping upon the creation of an index. 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):

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文件。现在,当我(就像你显示我)执行以下代码:

These fields from the content class (type,store etc.) come from the mapping.json file attached above. 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")

所有我作为responseResponse变量的响应都是:

All I get as a response on the searchResponse variable is:

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. 获取所有文档(无条件)

  2. 在一段时间内获取所有文档,基于关键字,例如服务

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

What am I doing wrong? I can provide with more information if needed.. Thank you all for your detailed answers.

推荐答案

你的C#POCO是不正确的关于你的映射;您的文档类型为sdoc属性

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.

这篇关于无法从弹性搜索中获取NEST的任何文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆