如何在nest 1.7中编写existsQuery

时间:2016-07-22 15:02:22

标签: elasticsearch nest

我正在使用nest 1.7,我需要编写此查询:

  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
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

1 个答案:

答案 0 :(得分:1)

使用流畅的API,方法调用几乎遵循查询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
                }
              }
            ]
          }
        }
      ]
    }
  }
}
默认情况下,

字段名称已经过驼峰,但您可以使用.SetDefaultPropertyNameInferrer(Func<string, string>)上的ConnectionSettings更改此行为。