如何在nest 1.7中编写existQuery [英] how to write existsQuery in nest 1.7

查看:36
本文介绍了如何在nest 1.7中编写existQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Nest 1.7,我需要编写以下查询:

I'm using nest 1.7 and i need to write this query:

  GET _search
{
   "from": 0,
   "size": 3,
   "query": {
      "bool": {
         "must": [
            {
               "constant_score": {
                  "filter": {
                     "bool": {
                        "must": [
                           {
                              "bool": {
                                 "must": [
                                    {
                                       "exists": {
                                          "field": "Collaborateurs"
                                       }
                                    },
                                    {
                                       "exists": {
                                          "field": "Collaborateurs.Nom"
                                       }
                                    },
                                    {
                                       "exists": {
                                          "field": "Collaborateurs.Fonction"
                                       }
                                    },
                                    {
                                       "exists": {
                                          "field": "Collaborateurs.TagVisuel"
                                       }
                                    },
                                    {
                                       "exists": {
                                          "field": "siAnnuaire"
                                       }
                                    },
                                    {
                                       "term": {
                                          "siAnnuaire": {
                                             "value": true
                                          }
                                       }
                                    },
                                    {
                                       "exists": {
                                          "field": "TagPhoto"
                                       }
                                    },
                                    {
                                       "range": {
                                          "NbAnnonce": {
                                             "gt": 0
                                          }
                                       }
                                    },
                                    {
                                       "geo_distance": {
                                          "distance": "10.0km",
                                          "AgenceLocation": {
                                             "lat": 48.8523513700019,
                                             "lon": 2.35127712591128
                                          }
                                       }
                                    }
                                 ]
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "function_score": {
                  "functions": [
                     {
                        "random_score": {
                           "seed": 69937385
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

推荐答案

使用流畅的API,方法调用几乎遵循查询DSL json的结构

Using the fluent API, the method calls follow pretty much the structure of the query DSL json

var response = client.Search<Document>(x => x
    .From(0)
    .Size(3)
    .Query(q => q
        .Bool(b => b
            .Must(m => m
                .ConstantScore(cs => cs
                    .Filter(csf => csf
                        .Bool(cb => cb
                            .Must(
                                cm => cm.Exists(p => p.Collaborateurs), 
                                cm => cm.Exists(p => p.Collaborateurs.Nom), 
                                cm => cm.Exists(p => p.Collaborateurs.Fonction)
                                // etc, etc for the other queries
                            )
                        )
                    )
                ), m => m 
                .FunctionScore(fs => fs
                    .Functions(fu => fu
                        .RandomScore(69937385)
                    )
                )
            )
        )
    )
);

产生

{
  "from": 0,
  "size": 3,
  "query": {
    "bool": {
      "must": [
        {
          "constant_score": {
            "filter": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "collaborateurs"
                    }
                  },
                  {
                    "exists": {
                      "field": "collaborateurs.nom"
                    }
                  },
                  {
                    "exists": {
                      "field": "collaborateurs.fonction"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "function_score": {
            "functions": [
              {
                "random_score": {
                  "seed": 69937385
                }
              }
            ]
          }
        }
      ]
    }
  }
}

字段名称默认情况下为驼峰式,但是您可以使用 ConnectionSettings 上的 .SetDefaultPropertyNameInferrer(Func< string,string>)更改此行为.

field names have been camel-cased by default, but you can change this behaviour using .SetDefaultPropertyNameInferrer(Func<string, string>) on ConnectionSettings.

这篇关于如何在nest 1.7中编写existQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆