[英]elasticsearch empty array search query with c# nest client
我在Elasticsearch中有一個這樣的模型:
"hits": [
{
"_index": "post",
"_type": "postmodel",
"_source": {
"projectId": "2",
"language": "en",
"postDate": "2017-06-11T08:39:32Z",
"profiles": [
{
"label": "Emotional",
"confidence": 1
}
]
}
},
{
"_index": "post",
"_type": "postmodel",
"_source": {
"projectId": "3",
"language": "en",
"postDate": "2017-06-11T08:05:01Z",
"profiles": []
}
},
...
通過使用c#Nest API,我想獲取具有空配置文件的postmodel(上面示例數據中的第二篇文章)。 我已經嘗試了許多方法來編寫正確的查詢,但是我在下面用此查詢結束了,但它仍然沒有給我正確的結果。 我當前的代碼如下所示:
var postModels = await _elasticClient.SearchAsync<PostModel>(s => s
.Index("post")
.Query(q =>
{
QueryContainer query = new QueryContainer();
query = query && q.Match(m => m.Field(f => f.ProjectId)
.Query("3"));
query = query && q.Nested(n => n.Path(p => p.Profiles)
.Query(qn => qn.Bool(b => b.Must(bm => bm.Match(m => m
.Field(f => f.Profiles).Query(null))))));
return query;
}));
如果有人可以幫助我解決這個問題,我將非常高興。 先感謝您。
這是一個完整的示例,說明如何使用must_not
子句中的exists
查詢來完成此must_not
private static void Main()
{
var defaultIndex = "post";
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
client.CreateIndex(defaultIndex, c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Mappings(m => m
.Map<PostModel>(mm => mm
.AutoMap()
.Properties(p => p
.Nested<Profile>(np => np
.Name(n => n.Profiles)
.AutoMap()
)
)
)
)
);
var posts = new List<PostModel>
{
new PostModel
{
ProjectId = "2",
Language = "en",
PostDate = new DateTime(2017, 6, 11, 8, 39, 32, DateTimeKind.Utc),
Profiles = new List<Profile>
{
new Profile
{
Label = "Emotional",
Confidence = 1
}
}
},
new PostModel
{
ProjectId = "3",
Language = "en",
PostDate = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc),
Profiles = new List<Profile>
{
new Profile
{
Label = "Lazy",
Confidence = 3
}
}
},
new PostModel
{
ProjectId = "3",
Language = "en",
PostDate = new DateTime(2017, 6, 11, 8, 5, 1, DateTimeKind.Utc),
Profiles = new List<Profile>()
}
};
client.IndexMany(posts);
client.Refresh(defaultIndex);
var postModels = client.Search<PostModel>(s => s
.Query(q =>
{
QueryContainer query = q
.Match(m => m
.Field(f => f.ProjectId)
.Query("3")
);
query = query && q
.Bool(b => b
.MustNot(bm => bm
.Nested(n => n
.Path(p => p.Profiles)
.Query(qn => qn
.Exists(m => m
.Field(f => f.Profiles)
)
)
)
)
);
return query;
}));
}
public class Profile
{
public string Label { get; set; }
public int Confidence { get; set; }
}
public class PostModel
{
public string ProjectId { get; set; }
public string Language { get; set; }
public DateTime PostDate { get; set; }
public List<Profile> Profiles { get; set; }
}
這僅返回一個帶有projectId 3
且沒有配置文件的文檔
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.47000363,
"hits" : [
{
"_index" : "post",
"_type" : "postmodel",
"_id" : "qeNF72ABeLoaZkUgC2Xh",
"_score" : 0.47000363,
"_source" : {
"projectId" : "3",
"language" : "en",
"postDate" : "2017-06-11T08:05:01Z",
"profiles" : [ ]
}
}
]
}
}
var postModels = client.Search<PostModel>(s => s
.Query(q => q
.Match(m => m
.Field(f => f.ProjectId)
.Query("3")
) && !q
.Nested(n => n
.Path(p => p.Profiles)
.Query(qn => qn
.Exists(m => m
.Field(f => f.Profiles)
)
)
)
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.