[英]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)
簡而言之,我想要做的就是:
我究竟做錯了什么? 如果需要,我可以提供更多信息。。謝謝大家的詳細回答。
您的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.