c# - 我的查询有什么问题吗.Nest elastic C#

标签 c# elasticsearch nest

我的 .Nest 库查询有问题吗?我的查询将获取所有数据,我需要按多个术语获取。 我想要的查询字符串弹性结果:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1000,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "log_query": {
         "doc_count": 2,
         "histogram_Log": {
            "buckets": [
               {
                  "key_as_string": "06/02/2015 12:00:00",
                  "key": 1423180800000,
                  "doc_count": 1
               },
               {
                  "key_as_string": "21/02/2015 12:00:00",
                  "key": 1424476800000,
                  "doc_count": 1
               }
            ]
         }
      }
   }
}

我的查询字符串弹性:

{
  "size": 0,
  "aggs": {
    "log_query": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "cluster": "giauht1"
              }
            },
            {
              "term": {
                "server": "hadoop0"
              }
            },
            {
              "term": {
                "type": "Warn"
              }
            },
            {
              "range": {
                "actionTime": {
                  "gte": "2015-02-01",
                  "lte": "2015-02-24"
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "histogram_Log": {
          "date_histogram": {
            "field": "actionTime",
            "interval": "1d",
            "format": "dd/MM/YYYY hh:mm:ss"
          }
        }
      }
    }
  }
}

我的 .nest 库查询:

 Func<SearchDescriptor<LogInfoIndexView>, SearchDescriptor<LogInfoIndexView>> query =
                que => que.Aggregations(aggs => aggs.Filter("log_query", fil =>
                {
                    fil.Filter(fb => fb.Bool(fm => fm.Must(
                        ftm =>
                        {
                            ftm.Term(t => t.Cluster, cluster);
                            ftm.Term(t => t.Server, server);
                            ftm.Term(t => t.Type, logLevel);
                            ftm.Range(r => r.OnField("actionTime").GreaterOrEquals(from.Value).LowerOrEquals(to.Value));
                            return ftm;
                        }))).Aggregations(faggs => faggs.DateHistogram("histogram_Log", dr =>
                    {
                        dr.Field("actionTime");
                        dr.Interval("1d");
                        dr.Format("dd/MM/YYYY hh:mm:ss");
                        return dr;
                    }));
                    return fil;
                })).Size(0).Type(new LogInfoIndexView().TypeName);
            var result = client.Search(query);

我的.nest 结果: .nest result

.nest result 2

我的模型映射:

{
   "onef-sora": {
      "mappings": {
         "FPT.OneF.Api.Log": {
            "properties": {
               "actionTime": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "application": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "cluster": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "detail": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "iD": {
                  "type": "string"
               },
               "message": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "server": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "source": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "tags": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "type": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "typeLog": {
                  "type": "string"
               },
               "typeName": {
                  "type": "string"
               },
               "url": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "user": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}

最佳答案

Must()条件传递给 Bool()过滤器采用 params Func<FilterDescriptor<T>, FilterContainer>[]但在你的过滤器中,Term()Range()过滤器被链接到同一个过滤器实例上;不幸的是,这并不像您可能期望的那样工作,最终结果实际上是一个传递给 must 的空 json 对象。过滤器查询 DSL 中的子句,即您最终得到

{
  "size": 0,
  "aggs": {
    "log_query": {
      "filter": {
        "bool": {
          "must": [
            {} /* where are the filters?! */
          ]
        }
      },
      "aggs": {
        "histogram_Log": {
          "date_histogram": {
            "field": "actionTime",
            "interval": "1d",
            "format": "dd/MM/YYYY hh:mm:ss"
          }
        }
      }
    }
  }
}

解决方案是传递一个 Func<FilterDescriptor<T>, FilterContainer> 的数组;以下匹配您的查询 DSL

void Main()
{
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
    var connection = new InMemoryConnection(settings);
    var client = new ElasticClient(connection: connection);

    DateTime? from = new DateTime(2015, 2,1);
    DateTime? to = new DateTime(2015, 2, 24);

    var docs = client.Search<LogInfoIndexView>(s => s
        .Size(0)
        .Type("type")
        .Aggregations(a => a
            .Filter("log_query", f => f
                .Filter(ff => ff
                    .Bool(b => b
                        .Must(m => m
                            .Term(t => t.Cluster, "giauht1"),
                              m => m
                            .Term(t => t.Server, "hadoop0"),
                              m => m
                            .Term(t => t.Type, "Warn"),
                              m => m
                            .Range(r => r.OnField("actionTime").GreaterOrEquals(from.Value).LowerOrEquals(to.Value))
                        )
                    )
                )
                .Aggregations(aa => aa
                    .DateHistogram("histogram_Log", da => da
                        .Field("actionTime")
                        .Interval("1d")
                        .Format("dd/MM/YYYY hh:mm:ss")
                    )
                )
            )
        )
    );

    Console.WriteLine(Encoding.UTF8.GetString(docs.RequestInformation.Request));
}

public class LogInfoIndexView
{
    public string Cluster { get; set; }
    public string Server { get; set; }
    public string Type { get; set; }
    public DateTime ActionTime { get; set; }    
}

回归

{
  "size": 0,
  "aggs": {
    "log_query": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "cluster": "giauht1"
              }
            },
            {
              "term": {
                "server": "hadoop0"
              }
            },
            {
              "term": {
                "type": "Warn"
              }
            },
            {
              "range": {
                "actionTime": {
                  "lte": "2015-02-24T00:00:00.000",
                  "gte": "2015-02-01T00:00:00.000"
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "histogram_Log": {
          "date_histogram": {
            "field": "actionTime",
            "interval": "1d",
            "format": "dd/MM/YYYY hh:mm:ss"
          }
        }
      }
    }
  }
}

编辑:

在回答您的评论时, filtered query filter 之间的区别和一个 filter aggregation 是前者在查询阶段开始时将过滤应用于所有文档并且过滤器通常被缓存,从而提高使用这些过滤器进行后续查询的性能,而后者在聚合范围内应用以过滤当前上下文中的文档以一个桶。如果您的查询只是为了执行聚合并且您可能会使用相同的过滤器运行聚合,我认为 filtered query filter应该提供更好的性能。

关于c# - 我的查询有什么问题吗.Nest elastic C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275650/

相关文章:

c# 数组函数参数传递一维

elasticsearch - Elasticsearch,Kibana分类/汇总文档

c# - Windows 8 XAML : Tint an Image object

c# - 在 C# 中,如何获取输出文件中每个文本行的文件位置(在写入时)?

c# - .net Core webapi 2(非 mvc)的 HttpResponseException/IHttpActionResponse 的等价物

elasticsearch - Elasticsearch无法启动,也没有日志centOS

elasticsearch - 用 `field`查询不返回任何内容

elasticsearch - Elasticsearch中两个类似的DSL查询之间的区别

c# - Elasticsearch 7和Nest 7-检索结果时,我收到键值对列表或空对象列表

elasticsearch - 字段搜索不返回字段值