Skip to content

Instantly share code, notes, and snippets.

@alexlarkou
alexlarkou / search_example.py
Created November 28, 2020 13:45
search example
from elasticsearch_dsl.search import Search
search = Search(using="default", index=index, doc_type=["doc"])
# we only want warning log in this instance
search = search.filter('term', level='warning')
# we look for results whose text includes the query "invalid argument"
search = search.query("multi_match", query="invalid argument", fields=["response.message", "stacktrace"], type="most_prefix")
@alexlarkou
alexlarkou / log_index.py
Last active August 28, 2022 15:20
Elasticsearch
from elasticsearch_dsl import Date, Document, Keyword, Nested, Text, Integer
class HttpLog(Document):
"""The schema for a Log document."""
level = Keyword()
request = Nested(properties={"body": Text(analyzer="snowball"), "headers": Keyword(), "arguments": Keyword()})
response = Nested(properties={"message": Text(analyzer="snowball"), "status": Integer()})
timestamp = Date()
// c#
int count = schools.count();
double average = allHeights.average()
// java
int count = schools.count();
OptionalDouble average = allHeights.average();
// c#
schools.ForEach(s => s.set_address("test address"));
// java
schools.forEach(s -> s.set_address("test address"));
// c#
IEnumerable<IGrouping<int, IEnumerable<Student>>> studentsGrouppedByYear = studentsInAllSchools.GroupBy(s => s.get_year());
// java
Map<Integer, List<Student>> studentsGrouppedByYear = studentsInAllSchools.collect(Collectors.groupingBy(s -> s.get_year()));
// c#
IEnumerable<Student> studentsInFirstYear = studentsInAllSchools.Where(s => s.get_year() == 1);
// java
Stream<Student> studentsInFirstYear = studentsInAllSchools.filter(s -> s.get_year() == 1);
// c#
bool doAllStudentsExceed100cmInHeight = studentsInAllSchools.All(s => s.get_height() > 100);
// java
boolean doAllStudentsExceed100cmInHeight = studentsInAllSchools.allMatch(s -> s.get_height() > 100);
// c#
bool doesThisStudentExist = studentsInAllSchools.Any(s => s.get_name() == "Alex");
// java
boolean doesThisStudentExist = studentsInAllSchools.anyMatch(s -> s.get_name() == "Alex");
// c#
List<Student> studentsInAllSchools = studentsInAllSchools.ToList();
// java
List<Student> studentsInAllSchoolsList = studentsInAllSchools.collect(Collectors.toList());
// or, if you specifically want an arrayList..
List<Student> studentsInAllSchoolsArrayList = studentsInAllSchools.collect(Collectors.toCollection(ArrayList::new));
// c#
IEnumerable<Student> studentsInAllSchools = schools.SelectMany(s => s.get_students());
// java
Stream<Student> studentsInAllSchools = schools.flatMap(s -> s.get_students());