If I look at this tweet, I can see that images are clearly attached. Yet I get the following:
>>> status = api.get_status(1275955989322162177, include_entities = True)
>>> status.entities
{'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https:///Ofq2EYjEPA', 'expanded_url': 'https://twitter.com/i/web/status/1275955989322162177', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [116, 139]}]}
As you can see, there are no images. What am I doing wrong?
It turns out that I needed to use the following flag:
status = api.get_status(1275955989322162177, include_entities = True, tweet_mode='extended')
Related
I'm trying to add support for multiple groups in my vehicles API. Currently we only support grouping by a single column like this.
Vehicle.group(:fuel_type).count
Which gives me a result like this:
{
"Petrol": 78,
"Diesel": 22
}
When I add multiple groups like this:
Vehicle.group(:fuel_type, :registration_status).count
I get the following result, which isn't as pretty in an API response. Also it's missing the combination Petrol and Exported since the count is 0.
{
"['Diesel', 'Scrapped']": 5,
"['Petrol', 'Registered']": 6,
"['Petrol', 'Scrapped']": 30,
"['Diesel', 'Registered']": 1,
"['Diesel', 'Deregistered']": 11,
"['Petrol', 'Deregistered']": 42,
"['Diesel', 'Exported']": 5
}
I would like it to be formatted like this instead:
{
"Diesel": {
"Scrapped": 5,
"Registered": 1,
"Deregistered": 11,
"Exported": 5
},
"Petrol": {
"Scrapped": 30,
"Registered": 6,
"Deregistered": 42,
"Exported: 0
}
}
Ideally I would like to support n nested groups, where every combination is displayed in every layer eg. even though there are no exported petrol cars, then it should still be included in the response with a count of 0.
I actually posted my question to openAI's ChatGPT and got a working implementation. Here is the snippet incase anyone has a similar issue:
def handle_hash(query)
return query unless query.is_a?(Hash)
result = {}
query.each do |key, value|
if key.is_a?(String)
result[key] = value
else
current = result
key[0...-1].each do |element|
current[element] ||= {}
current = current[element]
end
current[key[-1]] = value
end
end
result
end
There is an upper limit on the number of docs you can get from elastic search(that is 10000). we can use "scroll" to retrieve all the records. Does anyone know how to embed this in code?
There is this method scroll
https://github.com/elastic/elasticsearch-ruby/blob/4608fd144277941003de71a0cdc24bd39f17a012/elasticsearch-api/lib/elasticsearch/api/actions/scroll.rb
But I don't know how to use it. Could you explain how to use it?
I have tried the "scan". But it is no longer supported in Elasticsearch anymore.
# Open the "view" of the index
response = client.search index: 'test', search_type: 'scan', scroll: '5m', size: 10
# Call `scroll` until results are empty
while response = client.scroll(scroll_id: response['_scroll_id'], scroll: '5m') and not
response['hits']['hits'].empty? do
puts response['hits']['hits'].map { |r| r['_source']['title'] }
end
Your code should work, but as you mentioned the scan parameter for search_type is not necessary. I just ran this locally with some test data and it worked:
# scroll.rb
require 'elasticsearch'
client = Elasticsearch::Client.new
response = client.search(index: 'articles', scroll: '10m')
scroll_id = response['_scroll_id']
while response['hits']['hits'].size.positive?
response = client.scroll(scroll: '5m', body: { scroll_id: scroll_id })
puts(response['hits']['hits'].map { |r| r['_source']['title'] })
end
Output:
$ ruby scroll.rb
Title 297
Title 298
Title 299
Title 300
...
You can fiddle around with the value for the scroll parameter, but something like this should work for you too.
paragraph from elastic official docs :
We no longer recommend using the scroll API for deep pagination. If
you need to preserve the index state while paging through more than
10,000 hits, use the search_after parameter with a point in time
(PIT).
Scroll Official Doc Link
I recommend to use pagination.
you can use
that limitation in number of hits is for performance inprovements, you can use pagination, Its much faster.
in this way you can use start point with form key or use search_after key with sort and PIT(point in time for prevent from inconsistent result). and you can determinate you hits size key with 10 for faster query time.
Pagination Official Doc Link
for instantiate PIT ID:
POST /test/_pit?keep_alive=1m
for instantiate pagination:
GET /test/_search
{
"size": 10,
"query": {
"match" : {
"user.id" : "elkbee"
}
},
"pit": {
"id": "46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==",
"keep_alive": "1m"
},
"sort": [
{"#timestamp": {"order": "asc", "format": "strict_date_optional_time_nanos", "numeric_type" : "date_nanos" }}
]
}
for get rest of data in pagination:
there is sort key in the result, put it in the search_after
GET /test/_search
{
"size": 10,
"query": {
"match" : {
"user.id" : "elkbee"
}
},
"pit": {
"id": "46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==",
"keep_alive": "1m"
},
"sort": [
{"#timestamp": {"order": "asc", "format": "strict_date_optional_time_nanos"}}
],
"search_after": [
"2021-05-20T05:30:04.832Z", #you can find this value from sort key in response
4294967298
],
"track_total_hits": false
}
How can I initialize an array and be able to add nil value to it? as I know Array.wrap doesn't do the job.
list = Array.wrap(nil) => []
What I want:
list = Array.add(nil) => [nil]
Thank you
Try:
list = Array.new(1)
The number fed in as an argument dictates how many nils are added:
list = Array.new(3)
=> [nil, nil, nil]
Maybe you are looking for (Rails):
list = Array.wrap([nil])
#=> [nil]
But why not just a simple list = [nil], as per #engineersmnky's comment?
Also list = Array.new.push nil, but it's still better the easy way above.
I need an array that gives me #idea.id sorted by #idea.created_at.month
For example:
[[1,2,3], [4,5,6], [7,8,9], [], [], [], [], [], [], [], [], []]
where ids 1, 2, and 3 have #idea.created_at.month = 1 and so on through month = 12.
#ideas_by_month = Array.new(12){Array.new}
#ideas.each do |idea|
month = idea.created_at.month
#ideas_by_month[month-1] << idea.id
end
By the example, I'd need #ideas_by_month[0] to give me ids 1, 2, 3.
This currently adds all of the ideas into one slot [], and isn't sorting properly. How can I change it to make my array look like the example?
Array.new(12,[]) gives you 12 references to the same array. Array.new(12){Array.new} creates 12 different arrays.
The issue is not in your << call, but in your creation of the #ideas_by_month Array. From the Ruby API...
Array.new(3, true) #=> [true, true, true]
Note that the second argument populates the array with references to
the same object. Therefore, it is only recommended in cases when you
need to instantiate arrays with natively immutable objects such as
Symbols, numbers, true or false.
So, when you're pushing into any of the nested Arrays, it's all referencing the same space in memory. Which is why it appears that every id is getting pushed into every nested Array.
Instead declare your Array with a block:
#ideas_by_month = Array.new(12) { Array.new }
...which would look like this fully implemented as a class method:
idea.rb
class Idea < ActiveRecord::Base
...
def self.ideas_by_month
#ideas_by_month = Array.new(12){Array.new}
Idea.all.each do |idea|
month = idea.created_at.month
#ideas_by_month[month-1] << idea.id
end
return #ideas_by_month
end
...
end
Could someone point me in the right direction about looping through an array and extracting the required values. It's causing me issues understanding it:
array = [{"response_message"=>nil, "response_client_message"=>nil, "response_customer_message"=>nil, "closed"=>true, "updated_at"=>2012-05-30 13:20:49 UTC, "created_at"=>2012-05-30 13:20:29 UTC, "token"=>"2fda85eab962fa6e27850605f2f948ca", "price"=>"$24.00", "amount"=>#<BigDecimal:7fa3f4485428,'0.24E2',9(18)>, "currency_code"=>"USD", "metadata"=>"{\"xxx\": 5, \"xxx\": 250, \"xxx\": true, \"support\": { \"email\": true, \"phone\": false } }", "line_items"=>[{"amount"=>#<BigDecimal:7fa3f4482fe8,'0.24E2',9(18)>, "notes"=>nil, "currency_code"=>"USD", "description"=>"1 day", "price"=>"$24.00", "feature_level"=>"{\"hotspots\": 5, \"vouchers\": 250, \"customizable_logins\": true, \"support\": { \"email\": true, \"phone\": false } }", "metadata"=>"{\"hotspots\": 5, \"vouchers\": 250, \"customizable_logins\": true, \"support\": { \"email\": true, \"phone\": false } }"}]}]
One of these is generated each day, I need to loop through and present a few values. I've tried this:
array.select{|elem| elem[:updated_at]}
But that gives me a [].
How do I loop through this and extract the values?
I also need to understand how to get the line_items array out too.
Thanks
array.select{|elem| elem["updated_at"]}
pass string not a symbol
It's not entirely clear what you're after, but if you want to extract values from that array, try map:
array.map { |elem| elem["updated_at"] }
This will return you a list of all the "updated_at" values.
if you want to loop through an array and collect the updated_at values, then you should use the collect/map method.
In your example you are referencing elem[:updated_at] which is not the same as elem["updated_at"]
array.collect{|elem| elem["updated_at"]}