2016-11-15 37 views
2

我的目标是使用multiple searchElasticSearch巢Msearch FN并不能带来所有的结果

我使用NEST客户端使用函数发送查询到弹性搜索下方送一个ES请求3个独立的查询

IElasticClient _elasticClient.LowLevel.Msearch<string>(query).Body; 

传递作为原料查询中使用curl命令作品精美绝伦,但NEST MSearch仅返回“event_results”和“venue_results”,而不是“location_results”

curl -XPOST localhost:9200/_msearch -d ' 
{"index" : "search_results"} 
{ "size": 0, "query": { "bool": { "must": [ { "term": { "partnersites": "16" } }, { "match_phrase_prefix": { "name": "manchester" } } ] } }, "aggs": { "event_results": { "terms": { "field": "name.keyword", "size": 1 }, "aggs": { "top_tag_hits": { "top_hits": { "size": 1, "_source": [ "name", "groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "venueUrl", "media", "categories.name" ] } } } } } } 
{"index" : "search_results2"} 
{ "size": 0, "query": { "bool": { "must": [ { "term": { "partnersites": "16" } }, { "match_phrase_prefix": { "venueName": "Manchester" } } ] } }, "aggs": { "venue_results": { "terms": { "field": "name.keyword", "size": 1 }, "aggs": { "top_tag_hits": { "top_hits": { "size": 1, "_source": [ "name", "groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "venueUrl", "media", "categories.name" ] } } } } } } 
{"index" : "search_results3"} 
{ "size": 0, "query": { "bool": { "must": [ { "term": { "partnersites": "16" } }, { "match_phrase_prefix": { "venueTown": "manchester" } } ] } }, "aggs": { "location_results": { "terms": { "field": "name.keyword", "size": 1 }, "aggs": { "top_tag_hits": { "top_hits": { "size": 1, "_source": [ "name", "groupedName", "groupedDisplayName", "groupedUrl", "eventCode", "venueName", "venueTown", "venueId", "venueUrl", "media", "categories.name" ] } } } } } } 
' 

你们中的任何一个人都知道问题出在哪里?

回答

0

问题在于缩进。试试这个:

public static string qu = @"{""index"" : ""search_results""} 
{ ""size"": 0, ""query"": { ""bool"": { ""must"": [ { ""term"": { ""partnersites"": ""16"" } }, { ""match_phrase_prefix"": { ""name"": ""manchester"" } } ] } }, ""aggs"": { ""event_results"": { ""terms"": { ""field"": ""name.keyword"", ""size"": 1 }, ""aggs"": { ""top_tag_hits"": { ""top_hits"": { ""size"": 1, ""_source"": [ ""name"", ""groupedName"", ""groupedDisplayName"", ""groupedUrl"", ""eventCode"", ""venueName"", ""venueTown"", ""venueId"", ""venueUrl"", ""media"", ""categories.name"" ] } } } } } } 
{""index"" : ""search_results2""} 
{ ""size"": 0, ""query"": { ""bool"": { ""must"": [ { ""term"": { ""partnersites"": ""16"" } }, { ""match_phrase_prefix"": { ""venueName"": ""Manchester"" } } ] } }, ""aggs"": { ""venue_results"": { ""terms"": { ""field"": ""name.keyword"", ""size"": 1 }, ""aggs"": { ""top_tag_hits"": { ""top_hits"": { ""size"": 1, ""_source"": [ ""name"", ""groupedName"", ""groupedDisplayName"", ""groupedUrl"", ""eventCode"", ""venueName"", ""venueTown"", ""venueId"", ""venueUrl"", ""media"", ""categories.name"" ] } } } } } } 
{""index"" : ""search_results3""} 
{ ""size"": 0, ""query"": { ""bool"": { ""must"": [ { ""term"": { ""partnersites"": ""16"" } }, { ""match_phrase_prefix"": { ""venueTown"": ""manchester"" } } ] } }, ""aggs"": { ""location_results"": { ""terms"": { ""field"": ""name.keyword"", ""size"": 1 }, ""aggs"": { ""top_tag_hits"": { ""top_hits"": { ""size"": 1, ""_source"": [ ""name"", ""groupedName"", ""groupedDisplayName"", ""groupedUrl"", ""eventCode"", ""venueName"", ""venueTown"", ""venueId"", ""venueUrl"", ""media"", ""categories.name"" ] } } } } } } 
"; 

var result = _elasticClient.LowLevel.Msearch<string>(qu).Body; // Query ES for results. 
3

只要有可能,你都不想使用“LowLevel”的东西。相反,请使用IElasticClient上的可用功能。下面是如何使用IElasticClient.MultiSearch使用流利语法运行3次搜索的示例(这是执行此操作的首选方式)。

var mSearchResponse = ElasticClient.MultiSearch(msearch => msearch 
    .Search<RedemptionES>(
     s1 => s1.Query(
      q=>q.Term(
       t=> t.OnField(f=> f.Id).Value("123") 
      ) 
     ) 
    ) 
    .Search<RedemptionES>(
     s2 => s2.Query(
      q => q.Term(
       t => t.OnField(f => f.Id).Value("456") 
      ) 
     ) 
    ) 
    .Search<RedemptionES>(
     s3 => s3.Query(
      q => q.Term(
       t => t.OnField(f => f.Id).Value("789") 
      ) 
     ) 
    ) 
); 
+0

我的查询很庞大,转换为IELasticSearch语法需要很多时间,而且elasticsearch团队倾向于更改语法,所以它很难维护。是否有可能得到相同的结果,但使用原始查询? – ProgLearner