I've created an index in sense which I'm happy with and am trying to implement a typed query in the NEST client as follows:
我已经创建了一个我很满意的索引,并尝试在NEST客户端中实现类型化查询,如下所示:
-
var node = new Uri("http://elasticsearch-blablablamrfreeman");
var node = new Uri(“http:// elasticsearch-blablablamrfreeman”);
var settings = new ConnectionSettings(node) .SetTimeout(300000) .SetDefaultIndex("films") .MapDefaultTypeIndices(d => d .Add(typeof(film), "films")) .SetDefaultPropertyNameInferrer(p=>p);
-
Inject it (amongst the searcher and indexer) with my DI:
用我的DI注入它(在搜索者和索引器中):
builder.Register(c => new ElasticClient(settings)).Named<ElasticClient>("esclient");
-
Search using any query, such as the below:
使用任何查询进行搜索,例如:
var result = _client.Search<film>(s => s
.AllIndices() .From(0) .Size(10) .Query(q => q .Term(p => p.Title, query) ));var result = _client.Search
(s => s .AllIndices()。From(0).Size(10).Query(q => q .Term(p => p.Title,query)));
The indexer seems to work fine so code not included here. I've swapped in any number of settings parameters so I know that there's some redundancy in the code set above (or at least the default index would've sufficed).
索引器似乎工作正常所以代码不包括在这里。我已经交换了任意数量的设置参数,所以我知道上面的代码集中存在一些冗余(或者至少默认索引已经足够)。
The result var contains nothing whatsoever, with a big fat 0 across all it's properties, despite my having a wealth of data across my indices (including the "films" index).
结果var包含任何内容,尽管我的索引中包含大量数据(包括“电影”索引),但它的所有属性都有大胖0。
I've even tried a raw QueryRaw method with a matchall and nada!
我甚至尝试过使用matchall和nada的原始QueryRaw方法!
EDIT (Chris Pratt was along the right lines here)
编辑(克里斯普拉特在这里右侧)
Running:
var result = _client.Search<film>(s => s
.From(0)
.Size(10)
.QueryRaw(@"{ ""match_all"": {} }"));
And having:
var settings = new ConnectionSettings(node)
.SetTimeout(300000)
.MapDefaultTypeIndices(d => d
.Add(typeof (film), "chosen_index"))
.MapDefaultTypeNames(t => t
.Add(typeof (film), "en"));
Returns debug info as:
将调试信息返回为:
[Elasticsearch.Net.ElasticsearchResponse<Nest.SearchResponse<film>>] = {StatusCode: 200,
Method: POST,
Url: http://elasticsearch-blablablamrfreeman/chosen_index/film/_search,
Request: {
"from": 0,
"size": 10,
"query": { "match_all": {} }
},
Response: <Response stream not captured or already read...
My question being: It seemed I was in fact querying the wrong URL as per Chris Pratt's comment, but why isn't the type inference working for the type but it is for the index?
我的问题是:根据Chris Pratt的评论,似乎我实际上是在查询错误的URL,但为什么类型推断不适用于该类型,但它适用于索引?
/chosen_index/film/_search
should read
/chosen_index/en/_search
If my inferencing is correct.
如果我的推理是正确的。
Should it POST or GET? I usually GET via the search API on sense. And finally, what if I want to write my queries against my native film type but have it override the ES-type in the URL in some instances.
应该POST还是GET?我通常在感觉上通过搜索API获取。最后,如果我想针对我的原生电影类型编写查询,但在某些情况下让它覆盖URL中的ES类型,该怎么办?
For example if I inject a different language parameter and wish to now query the same index but both "en" and "de" ES-types etc (which are all valid types under the same index as already constructed via sense).
例如,如果我注入一个不同的语言参数,并希望现在查询相同的索引,但同时查询“en”和“de”ES类型等(它们都是通过sense构建的相同索引下的所有有效类型)。
Thanks in advance!
提前致谢!
2 个解决方案
#1
Nothing obvious is jumping out at me for why this isn't working for you. However, I can give you a few avenues to pursue to attempt to resolve the issue.
没有什么是显而易见的,因为这不适合你。但是,我可以为您提供一些尝试解决问题的途径。
-
I'm not familiar with the particular DI container that you're using, but it's possible that it's not binding properly, resulting some of your settings options not actually being utilized in the instance that's created. Might be a long shot, but I'd recommend digging in and at least verifying that the client instance you're getting is setup the way it should be.
我不熟悉您正在使用的特定DI容器,但它可能没有正确绑定,导致您的某些设置选项实际上并未在创建的实例中使用。可能是一个很长的镜头,但我建议深入研究并至少验证您所获得的客户端实例是否按照应有的方式进行设置。
-
It sort of side-steps the issue in a way, but Elasticsearch explicitly recommends you don't handle localization via different types. You should either use different indexes, i.e.
chosen_index_en
,chosen_index_es
, etc., or use multifields:它在某种程度上可以解决问题,但Elasticsearch明确建议您不要通过不同类型处理本地化。您应该使用不同的索引,即selected_index_en,chosen_index_es等,或使用多字段:
"title": { "type": "string", "fields": { "en": { "type": "string", "analyzer": "english" }, "es": { "type": "string", "analyzer": "spanish" } }
Then you can search on things like
title.en
ortitle.es
.然后你可以搜索title.en或title.es之类的东西。
#2
As I see you are using the default mappings for the film type. That is, the data are analyzed by the standard analyzer before being indexed.
我看到你正在使用电影类型的默认映射。也就是说,数据在被索引之前由标准分析器分析。
In the query, you are using the Term query which finds documents that contain the exact term (not analyzed) specified in the inverted index (see here). So be careful what your query is.
在查询中,您正在使用Term查询,该查询查找包含在倒排索引中指定的确切术语(未分析)的文档(请参阅此处)。所以要小心你的查询。
Try to use a match query like below:
尝试使用如下匹配查询:
var result = _client.Search<film>(s => s
.AllIndices()
.From(0)
.Size(10)
.Query(q => q
.Match(p => p.Title, query)
));
The query is now analyzed by the standard analyzer before being applied (see here).
现在,在应用之前,标准分析器会对查询进行分析(请参阅此处)。
#1
Nothing obvious is jumping out at me for why this isn't working for you. However, I can give you a few avenues to pursue to attempt to resolve the issue.
没有什么是显而易见的,因为这不适合你。但是,我可以为您提供一些尝试解决问题的途径。
-
I'm not familiar with the particular DI container that you're using, but it's possible that it's not binding properly, resulting some of your settings options not actually being utilized in the instance that's created. Might be a long shot, but I'd recommend digging in and at least verifying that the client instance you're getting is setup the way it should be.
我不熟悉您正在使用的特定DI容器,但它可能没有正确绑定,导致您的某些设置选项实际上并未在创建的实例中使用。可能是一个很长的镜头,但我建议深入研究并至少验证您所获得的客户端实例是否按照应有的方式进行设置。
-
It sort of side-steps the issue in a way, but Elasticsearch explicitly recommends you don't handle localization via different types. You should either use different indexes, i.e.
chosen_index_en
,chosen_index_es
, etc., or use multifields:它在某种程度上可以解决问题,但Elasticsearch明确建议您不要通过不同类型处理本地化。您应该使用不同的索引,即selected_index_en,chosen_index_es等,或使用多字段:
"title": { "type": "string", "fields": { "en": { "type": "string", "analyzer": "english" }, "es": { "type": "string", "analyzer": "spanish" } }
Then you can search on things like
title.en
ortitle.es
.然后你可以搜索title.en或title.es之类的东西。
#2
As I see you are using the default mappings for the film type. That is, the data are analyzed by the standard analyzer before being indexed.
我看到你正在使用电影类型的默认映射。也就是说,数据在被索引之前由标准分析器分析。
In the query, you are using the Term query which finds documents that contain the exact term (not analyzed) specified in the inverted index (see here). So be careful what your query is.
在查询中,您正在使用Term查询,该查询查找包含在倒排索引中指定的确切术语(未分析)的文档(请参阅此处)。所以要小心你的查询。
Try to use a match query like below:
尝试使用如下匹配查询:
var result = _client.Search<film>(s => s
.AllIndices()
.From(0)
.Size(10)
.Query(q => q
.Match(p => p.Title, query)
));
The query is now analyzed by the standard analyzer before being applied (see here).
现在,在应用之前,标准分析器会对查询进行分析(请参阅此处)。