2016-12-15 31 views
0

我使用Searchblox来索引和搜索我的文件,这些文件本身称为ES 2.x来完成这项工作。 Searchblox在创建索引时使用“mapping.json”文件来初始化映射。这是该文件的link。作为“@Russ凸轮”建议here,我创建了自己的类的内容用下面的代码(像他一样的“问题”指数和“问题”类):无法从elasticsearch获取NEST的任何文档

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. 得到一个时间范围内的所有文件和基于关键词的..如“服务”

我在做什么错?如有需要,我可以提供更多信息。谢谢大家的详细解答。

+0

我调试了以下行: var searchResponse = highLevelclient.Search (s => query(q => q .Match(m => m.Field(f => f.fields.content).Query(“服务“) 并得到以下内容:[error](https://drive.google.com/open?id=0B1LnMWaj1afaQ0ltd0ExVGc3OTQ)。 为什么我得到_“方法只能在Type.IsGenericParameter为true的类型上调用。”_错误?这个错误是否必须对我执行任何操作** get ** **文档? – cemowski23

+0

您最好直接使用Searchblox api获取json或xml格式的文档。 – user7313404

+0

此外,请检查基本身份验证凭据以访问elasticsearch @ 9200,因为Searchblox在访问数据之前提供了一个安全层。 – user7313404

回答

1

您的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"场为了命名它以匹配映射(我们可以在这里进一步讨论并设置附加属性值来完全匹配映射; see the automapping documentation)。

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

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 types were removed in Elasticsearch 1.0(多字段仍然存在,只是实际类型不是),所以请确保您实际上在Searchblox上运行Elasticsearch 2.x,因为NEST 2.x仅支持Elasticsearch 2.x.

相关问题