我使用Searchblox索引和搜索我的文件,它本身调用ES 2.x来完成这项工作。 Searchblox使用“mapping.json”文件在创建索引时初始化映射。这是该文件的link。正如“@Russ Cam”建议here,我使用以下代码创建了我自己的类内容(就像他对“问题”索引和“问题”类所做的那样):
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)
简而言之,我想要的只是:
我做错了什么?如果需要,我可以提供更多信息。谢谢大家的详细解答。
答案 0 :(得分: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