Elasticsearch - find multiple exact values - elasticsearch

https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html
{
"terms" : {
"price" : [20, 30]
}
}
What is the max number of values we can put in this search?
"price" : [20, 30, 40, 50, 10, 11, 12 .... to the max number of search values]
Thanks!

searain,
The max number of terms is 1024, you can still change it for a higher number
using (in case if you are using it into a bool operator):
indices.query.bool.max_clause_count
into the .yml config file.

Related

How to check if any element of an array is greater than 100?

This should return true
array = [30, 40, 50, 100]
This should return false:
array = [10, 20, 30, 40]
Does a predefined function exist?
Use any?
[30,40,50,100].any? { |item| item >= 100 } # => true
[10,20,30,40].any? { |item| item >= 100 } # => false
Note that even in your first example none of the elements is greater than 100, I took for granted you meant greater than or equals to 100
I wanted to find the first number that is greater than 100 on the array. If you are here for the same, then find the answers below
[30, 40, 50, 100, 110, 120].find { |n| n > 100 }
#=> 110
And if you want to find all the numbers greater than 100
[30, 40, 50, 100, 110, 120].find_all { |n| n > 100 }
#=> [110, 120]

What is the ..= (dot dot equals) operator in Rust?

I saw this ..= operator in some Rust code:
for s in 2..=9 {
// some code here
}
What is it?
This is the inclusive range operator.
The range x..=y contains all values >= x and <= y, i.e. “from x up to and including y”.
This is in contrast to the non-inclusive range operator x..y, which doesn't include y itself.
fn main() {
println!("{:?}", (10..20) .collect::<Vec<_>>());
println!("{:?}", (10..=20).collect::<Vec<_>>());
}
// Output:
//
// [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
// [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Match expressions
You can also use start..=end as a pattern in a match expression to match any value in the (inclusive) range.
match fahrenheit_temperature {
70..=89 => println!("What lovely weather!"),
_ => println!("Ugh, I'm staying in."),
}
(Using an exclusive range start..end as a pattern is an experimental feature.)
History
Inclusive ranges used to be an experimental nightly-only feature, and were written ... before.
As of Rust 1.26, it's officially part of the language, and written ..=.
(Before inclusive ranges existed, you actually couldn't create, say, a range of byte values including 255u8. Because that'd be 0..256, and 256 is out of the u8 range! This is issue #23635.)
See also
The Rust 1.26 release blog post's introduction to ..=.
StackOverflow: How do I include the end value in a range?

Is there a way of fixing a maximum value for an element in an array?

list = [5, 10, 20, 40, 50, 42, 35, 26, 18]
So in this example, I don't want an element to exceed the value 40 and if it does, I want the element to have a value of 40, e.g. the element with the value 50 will become 40.
list.map! { |i| i > 40 ? 40 : i }
=> [5, 10, 20, 40, 40, 40, 35, 26, 18]
map method permits you to apply a transformation to all the elements of your collection so I think fits perfectly in this case.
Or, if you are populating your array one element at a time, you could insert the real value just if it's 40 or less, 40 otherwise.
If 40 is the max, you need the min from 40 and the element ;)
list = [5, 10, 20, 40, 50, 42, 35, 26, 18]
list.map{ |v| [40, v].min }
# => [5, 10, 20, 40, 40, 40, 35, 26, 18]
If you're using Ruby 2.4 or newer, you can make use of Comparable#clamp:
list.map { |n| n.clamp(0, 40) }
# => [5, 10, 20, 40, 40, 40, 35, 26, 18]
Note that this will also fix the minimum value to 0. This may or may not be useful in your case, and only applies if all items are expected to be non-negative numbers.

Lexicographically sort sequences of integers with LINQ

I'm trying to find an elegant way to lexically sort sequences of integers with LINQ. In other words, If I have these sequences of ints
7, 10, 12, 14, 15
10, 12, 15
10
7, 15
14, 15
I would hope to have them come out sorted like this
7, 10, 12, 14, 15
7, 15
10
10, 12, 15
14, 15
It is the same basic idea of sorting strings by their characters except I want to sort a sequence of ints instead. I don't want to sort alphabetically, but I do want the sequences lexically sorted.
The kind of sort you want is known as a lexical sort:
Given two partially ordered sets A and B, the lexicographical order on
the Cartesian product A × B is defined as (a,b) ≤ (a′,b′) if and only
if a < a′ or (a = a′ and b ≤ b′).
.Net gives you the tools to specify what kind of comparison you want when you want to sort. There are two structures to control this: IComparer and the Comparison<T> delegate. You can pass either one of these to List.Sort. Example:
var lists = new List<Int32[]> {
new [] { 7, 10, 12, 14, 15 },
new [] { 7, 15 },
new [] { 7, 15 },
new [] { 10 },
new [] { 10, 12, 15 },
new [] { 14, 15 } };
lists.Sort((a, b) => {
var result = a.Zip(b, Tuple.Create)
.Select(t => t.Item1.CompareTo(t.Item2))
.FirstOrDefault(c => c != 0);
return result == 0 && !a.Any() ? -1 : result; // Empty list minimum
});
(Download for LinqPad)
This passes a Comparison<Int32[]> delegate which zips the comparands, allowing element by element comparison and stops comparing when the first non-zero integer comparison is detected. If no unequal elements are found, it returns the default for Int32 which is 0, meaning the lists are lexicographically equal.
(Note I added another element to your set of lists to show that equal lists of integers sort correctly.)
Bonus chatter:
I thought this method would be faster than allocating strings and using OrderBy but after profiling there's no appreciable difference in speed. I tried to make it faster using a struct instead of Tuple, which did help a little, and probably saved GC allocation and memory usage, but I didn't measure memory usage performance. If performance is a concern, you'd probably end up eschewing the tidy Linq approach and write the Comparer using a loop.
You could convert the numbers to strings, pad them all to the same length and join the together, then order that, and finally split them up again.
var intLists = new List<List<int>>
{
new List<int> { 7, 10, 12, 14, 15 },
new List<int> { 10, 12, 15 },
new List<int> { 10 },
new List<int> { 7, 15 },
new List<int> { 14, 15 },
};
var orderedLists = intLists
.Select(l => string.Join("", l.Select (x => x.ToString().PadLeft(10))))
.OrderBy(l => l)
.Select(l => l.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select (x => int.Parse(x)));

Summing and comparing arrays in MongoDB

I'm very new to mongodb, I've done simple stuff like storing and retrieving documents.
I have a collection of documents (thousands and growing) with and embedded array of integers (can be as large as 5000 integers) between 0 and 255
Example Mongo Collection Data:
{
"name": "item1",
"values": [1, 93, 45, 67, 89, 1, 2, 32, 45]
},
{
"name": "item2",
"values": [1, 23, 45, 123, 1, 5, 89, 14, 22]
},
{
"name": "item3",
"values": [23, 1, 44, 78, 89, 22, 150, 23, 12]
},
{
"name": "item4",
"values": [90, 23, 11, 67, 29, 1, 2, 1, 45]
}
Comparison would be:
pseudo code:
distance = 0
for a in passed_in_item
for b in mongo_collection
distance += a - b
end
end
an example passed in array (same as the ones in the mongo document, they will always be the same length):
[1, 93, 45, 67, 89, 1, 2, 32, 45]
I'd like to pass in an array of integers as a query and difference it against the array in the document to find the one with the least difference. Is this the sort of thing map reduce is good at and how would I roughly go about it? An example would be great. Also eventually I'd like the passed in array to come from another document in Mongo in a different collection.
Thanks!

Resources