C# 4.0 如何在C嵌套弹性搜索中动态添加多个布尔条件?
我正在使用C Nest Elastic search 1.X版本。我正在为弹性搜索查询编写C代码。我的要求是我需要构建动态bool查询。我已经尝试了很多方法,但代码无法实现。请帮帮我。请查找下面的示例Elastic search Json查询和我试图实现示例Json查询的C代码 Json查询:C# 4.0 如何在C嵌套弹性搜索中动态添加多个布尔条件?,c#-4.0,nest,C# 4.0,Nest,我正在使用C Nest Elastic search 1.X版本。我正在为弹性搜索查询编写C代码。我的要求是我需要构建动态bool查询。我已经尝试了很多方法,但代码无法实现。请帮帮我。请查找下面的示例Elastic search Json查询和我试图实现示例Json查询的C代码 Json查询: { "from":0, "size":20, "query":{ "filtered":{ "query":{ "b
{
"from":0,
"size":20,
"query":{
"filtered":{
"query":{
"bool":{
"must":[
{
"multi_match":{
"type":"phrase",
"query":"Singapore",
"slop":0,
"fields":[
"title^2",
"content",
"summary^1.8"
]
}
},
{
"bool":{
"should":[
{
"multi_match":{
"query":" www.channelnewsasia.com",
"fields":[
"host"
]
}
},
{
"bool":{
"must":[
{
"multi_match":{
"type":"phrase",
"query":"real",
"slop":0,
"fields":[
"title^2",
"content",
"summary^1.8"
]
}
},
{
"multi_match":{
"type":"phrase",
"query":"estate",
"slop":0,
"fields":[
"title^2",
"content",
"summary^1.8"
]
}
},
{
"multi_match":{
"query":" www.scmp.com",
"fields":[
"host"
]
}
}
]
},
"bool":{
"must":[
{
"multi_match":{
"type":"phrase",
"query":"real",
"slop":0,
"fields":[
"title^2",
"content",
"summary^1.8"
]
}
},
{
"multi_match":{
"type":"phrase",
"query":"estate",
"slop":0,
"fields":[
"title^2",
"content",
"summary^1.8"
]
}
},
{
"multi_match":{
"query":" www.bbc.com",
"fields":[
"host"
]
}
}
]
}
}
]
}
}
]
}
}
}
}
}
C代码:
ISearchResponse<JLLNews> oResults =
_ESClient.Search<JLLNews>(s => s
.From(pageOffset * 20)
.Size(20)
.AllTypes()
.Index(sIndex)
.Query(q => q
.Filtered(qs => qs
.Query(r => r
.Bool(b => b
.Must(GetFieldQueryContainer(sInputSearch, oFields,newsWebsites, oFieldsFtr).ToArray()
)
)
);
public List<QueryContainer> GetFieldQueryContainer(string sInputSearch, IEnumerable<string> oFields,string newsWebsites=null, IEnumerable<string> oFieldsFtr =null)
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"((""((?<token>.*?)(?<!\\)"")|(?<token>[\w]+))(\s)*)", options);
//string input = @"Here is my string' it 'has' ""what six matches"" ";
var result = (from Match m in regex.Matches(sInputSearch)
where m.Groups["token"].Success
select m.Groups["token"].Value).ToList();
var qc = new List<QueryContainer>();
foreach (var term in result)
{
qc.Add(
Query<JLLNews>.MultiMatch(qt => qt
.Query(term)
.Type(TextQueryType.Phrase)
.Slop(0)
.OnFields(oFields)
));
}
qc.Add(Query<JLLNews>.Bool(b => b.Should(sh => sh
.MultiMatch(mm => mm
.Query(newsWebsites)
.OnFields(oFieldsFtr)
))));
return qc;
}
谢谢
Srinivasa RaoJSON查询有3级布尔查询,但C代码只有2级。从一次性构建查询开始,然后将常见的构建块抽象成方法,可能会更容易。看看编写bool查询文档中的一些指针。运算符重载将使构建变得更加容易:JSON查询有3个bool查询级别,但C代码只有2个。从一次性构建查询开始,然后将常见的构建块抽象成方法,可能会更容易。看看编写bool查询文档中的一些指针。运算符重载将使构建更容易: