2016-12-15 30 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.

相關問題