Let say I have following 3 documents in ES index.
{
someId: 'A'
}
{
someId: 'B'
}
{
someId: 'C'
}
Let's say I have an array of someId ['A', 'B', 'C', 'D', 'E']. What I need to find is which of elements in the array is in ES.
For the above example ES contains document for someId ['A', 'B', 'C'] but not for ['D', 'E']
Is there an ES query to achieve it?
Related
This question already has answers here:
What does the "map" method do in Ruby?
(8 answers)
Closed 9 months ago.
I want to map values from a named list in Ruby. Is there an equivalent map function in Ruby? I have an array
letters = ['a', 'b', 'c', 'd']
capital = letters .map { l, l .capitalize] }
puts capital
Yes, Ruby arrays have a .map method that you can call (https://ruby-doc.org/core-2.7.5/Array.html#method-i-map).
What you probably want is:
letters = ['a', 'b', 'c', 'd']
capitals = letters.map {|letter| letter.capitalize}
or you could also use the shorter form:
letters = ['a', 'b', 'c', 'd']
capitals = letters.map(&:capitalize)
or maybe even use the .upcase instead of .capitalize if all you need for the result is uppercase.
I have an array with arrays nested in it. I tried to test it with Chai, but it doesn't pass the test. I have checked that the value in both of these arrays are correct.
const mainArray = [
['f', 'r', 'e', 'e'], ['b', 'e', 'e']
]
const targetArray = ['b', 'e', 'e']
expect(mainArray).to.include(targetArray) //False, expect it to be True
How can I test this correctly?
You can use .deep.members
chai.expect(mainArray).to.include.deep.members([targetArray])
When doing .to.include(targetArray), its looking in the mainArray to have the members in targetArray. So its looking for b, e inside mainArray
It should be like running uniq command, but by columns. For example:
A B C B
A C B C
A A A A
Second and fourth columns are identical. Which is the best way to obtain the following result?
A B C
A C B
A A A
However, at first it is unknown which columns are identical, much like with the uniq command for rows.
Perl to the rescue!
perl -lane '
push #{ $c[$_] }, $F[$_] for 0 .. $#F;
}{
for (#c) {
$s = join "|", #$_;
$seen{$s}++ or push #r, $_;
}
print join " ", map shift #$_, #r while #{ $r[0] }
' -- inputfile
The first line pivots the input, i.e. it creates the following structure:
#c = ( [ 'A', 'A', 'A' ],
[ 'B', 'C', 'A' ],
[ 'C', 'B', 'A' ],
[ 'B', 'C', 'A' ] );
The }{ (called "Eskimo greeting") separates the code run for each line from the code run after the whole input has been processed. It walks the #c array and only keeps the unique columns (by creating a string from each of them like A|A|A, B|C|A, etc. and storing them in the %seen hash.
The structure will be
#r = ( [ 'A', 'A', 'A' ],
[ 'B', 'C', 'A' ],
[ 'C', 'B', 'A' ] );
and the hash will look like
%seen = ( 'B|C|A' => 2,
'A|A|A' => 1,
'C|B|A' => 1
);
The last print shifts the first element of each column, i.e. it pivots the result back.
I have some data to sort as an array in the form ['a1', 'b321', 'a33', 'c', ...].
I want to put all the 'aN' into sorted_data[:a] etc.
The code below loops through the data, and correctly runs the regexp on them.
What it doesn't do is put them in the right place - sorted_data[filter[:key]] is null.
How do use filter[:key] as a key to sorted_data?
Thanks.
sorted_data = { a: Array.new,
b: Array.new,
c: -1 }
filters = [{ re: /^a\d+$/, key: 'a' },
{ re: /^b\d+$/, key: 'b' },
{ re: /^c$/, key: 'c' }]
['a1', 'b321', 'a33', 'c', 'b', 'b1'].each {|cell|
filters.each {|filter|
if cell.match(filter[:re])
puts "#{cell} should go in #{filter[:key]}" + '....[' + sorted_data[filter[:key]].to_s + ']....'
break
end
}
}
The output of the above is
# a1 should go in a....[]....
# b321 should go in b....[]....
# a33 should go in a....[]....
# c should go in c....[]....
# b1 should go in b....[]....
I believe the program below produces the desired output:
l = ['a1', 'b321', 'a33', 'c', 'b', 'b1']
sorted_data = Hash.new { |hash, key| hash[key] = [] }
l.each do |item|
first_char = item[0].to_sym
sorted_data[first_char].push item
end
puts sorted_data
Output:
{:a=>["a1", "a33"], :b=>["b321", "b", "b1"], :c=>["c"]}
I have an array like this:
['a', 'b', 'c']
What is the simplest way to turn it into:
{'a' => true, 'b' => true, 'c' => true}
true is just a standard value that values should hold.
How about below ?
2.1.0 :001 > ['a', 'b', 'c'].each_with_object(true).to_h
=> {"a"=>true, "b"=>true, "c"=>true}
Try:
Hash[ary.map {|k| [k, true]}]
Since Ruby 2.0 you can use to_h method:
ary.map {|k| [k, true]}.to_h
Depending on your specific needs, maybe you do not actually need to initialize the values. You could simply create a Hash with a default value of true this way:
h = Hash.new(true)
#=> {}
Then, when you try to access a key that was not present before:
h['a']
#=> true
h['b']
#=> true
Pros: less memory used, faster to initialize.
Cons: does not actually store keys so the hash will be empty until some other code stores values in it. This will only be a problem if your program relies on reading the keys from the hash or wants to iterate over the hash.
['a', 'b', 'c'].each_with_object({}) { |key, hash| hash[key] = true }
Another one
> Hash[arr.zip Array.new(arr.size, true)]
# => {"a"=>true, "b"=>true, "c"=>true}
You can also use Array#product:
['a', 'b', 'c'].product([true]).to_h
#=> {"a"=>true, "b"=>true, "c"=>true}
Following code will do this:
hash = {}
['a', 'b', 'c'].each{|i| hash[i] = true}
Hope this helps :)
If you switch to Python, it's this easy:
>>> l = ['a', 'b', 'c']
>>> d = dict.fromkeys(l, True)
>>> d
{'a': True, 'c': True, 'b': True}