如何在C#nest弹性搜索中动态添加多个bool条件?

时间:2017-07-20 10:18:01

标签: c#-4.0 nest

我正在使用C#Nest Elastic search 1.X版本。我正在为弹性搜索查询编写C#代码。我的要求是我需要构建动态bool查询。我尝试了很多方法,但代码得到了实现。请帮我。请找到以下示例弹性搜索Json查询和C#代码,我试图实现示例Json查询。

Json查询:

{  
   "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 Rao

0 个答案:

没有答案