set maxResults with JIRA REST API - jira

I wonder how to set maxResults using jql?
Here is my query:
search?jql=project%20=%20SYTLK%20AND%20maxResults=500
And here is the result:
{"errorMessages":["Field 'maxResults' does not exist or you do not have permission to view it."],"errors":{}}

maxResults is a query parameter, not a JQL-related keyword.
/search?jql=[JQL_string]&maxResults=500

Maybe you can try post type for your request!
Json format:
{
"jql": "project = HSP",
"startAt": 0,
"maxResults": 15,
"fields": [
"summary",
"status",
"assignee"
]
}
Ref: https://docs.atlassian.com/software/jira/docs/api/REST/7.10.1/#api/2/search-searchUsingSearchRequest

Related

How to properly query Postgresql JSONB array of hashes on Ruby on Rails 6?

This is my column:
[
{ id: 1, value: 1, complete: true },
{ id: 2, value: 1, complete: false },
{ id: 3, value: 1, complete: true }
]
First, is there a "correct" way to work with a jsonb scheme? should I redesign to work with a single json instead of the array of hashes?
I have about 200 entries on the database, the column status has 200 of those itens.
How would I perform a query to get the count of true/false?
How can I query for ALL complete itens? I can query for the database rows in which the json has an item complete, but I can't query for all the itens, in all rows of the database that are complete.
Appreciate the help, thank you
Aha! I found it here:
https://levelup.gitconnected.com/how-to-query-a-json-array-of-objects-as-a-recordset-in-postgresql-a81acec9fbc5
Say your dataset is like this:
[{
"productid": "3",
"name": "Virtual Keyboard",
"price": "150.00"
}, {
"productid": "1",
"name": "Dell 123 Laptop Computer",
"price": "1300.00"
},
{
"productid": "8",
"name": "LG Ultrawide Monitor",
"price": "190.00"
}]
The proper way to count it, is like this:
select items.name, count(*) as num from
purchases,jsonb_to_recordset(purchases.items_purchased) as items(name text)
group by items.name
order by num Desc
Works like a charm and is extremely fast.
To do it in Rails, you need to use Model.find_by_sql(....) and indicate your select therem. I'm sure there are probably better ways to do it.

How to retreive all the records from elasticsearch in rails

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
}

Create Nested Build for object rails

My JSON API is like below,
{ "schedule_id": "1",
"latitude" : 17.4327,
"longitude" : 78.4302,
"device_id": "123test",
"audit_compliances":[
{
"value": "Yes",
"score": 10,
"remarks": "some remarks",
"private_remarks": "some remarks",
"check_point_id": 1,
"audit_compliance_documents":[{
"score": 10,
"remarks": "some remarks",
}]
}]
i have a relations for that DB i want to save all this records at once so i want to initialize the object with details and build inner objects along with that. Started building like this but how can i build inner build for documents.
submission = Submission.new(audit_schedule_id: params[:schedule_id],
latitude: params[:latitude], longitude: params[:longitude],
device_id: params[:device_id])
params[:audit_compliances].each do |audit_compliance|
submission.audit_compliances.build(
value: audit_compliance[:value],
score: audit_compliance[:score],
remarks: audit_compliance[:remarks],
private_remarks: audit_compliance[:private_remarks],
check_point_id: audit_compliance[:check_point_id])
end
pass your json param in submission params directly
like this
params[:submission] = json_params;
Submission.create(params[:submission]);
It will create both if your mapping is correct........

Remove metadata from json results

I'm using ActiveResource to establish a REST connection with rails 4.2 to an ADS Advantage server using the WebPlatform from ADS. It returns json with "__metadata". How can I remove the "__metadata"?
{
"__metadata": {
"uri": "http://.....",
"key_fields": "ID",
"rows_affected": 0,
"last_autoinc": 0
},
In my class I have added self.include_format_in_path = false, to remove the .json from the end of the uri.
Thanks.
you can achieve this in the following steps:
parse the JSON:
parsed_json = JSON.parse('{ "__metadata": { "uri": "http://.....", "key_fields": "ID", "rows_affected": 0, "last_autoinc": 0 }}')
then you will get a hash type and you just need to get the inside of __metadata:
result = parsed_json['__metadata']
then you can just return it or print it:
puts result.to_json
#=> {"uri"=>"http://.....", "key_fields"=>"ID", "rows_affected"=>0, "last_autoinc"=>0}

Search a JSON Response using Ruby

I'm using a Ruby script to interface with an application API and the results being returned are in a JSON format. For example:
{
"incidents": [
{
"number": 1,
"status": "open",
"key": "abc123"
}
{
"number": 2,
"status": "open",
"key": "xyz098"
}
{
"number": 3,
"status": "closed",
"key": "lmn456"
}
]
}
I'm looking to search each block for a particular "key" value (yzx098 in this example) and return the associated "number" value.
Now, I'm very new to Ruby and I'm not sure if there's already a function to help accomplish this. However, a couple days of scouring the Googles and Ruby resource books hasn't yielded anything that works.
Any suggestions?
First of all, the JSON should be as below: (note the commas)
{
"incidents": [
{
"number": 1,
"status": "open",
"key": "abc123"
},
{
"number": 2,
"status": "open",
"key": "xyz098"
},
{
"number": 3,
"status": "closed",
"key": "lmn456"
}
]
}
Strore the above json in a variable
s = '{"incidents": [{"number": 1,"status": "open","key": "abc123"},{"number": 2,"status": "open","key": "xyz098"},{"number": 3,"status": "closed","key": "lmn456"}]}'
Parse the JSON
h = JSON.parse(s)
Find the required number using map
h["incidents"].map {|h1| h1['number'] if h1['key']=='xyz098'}.compact.first
Or you could also use find as below
h["incidents"].find {|h1| h1['key']=='xyz098'}['number']
Or you could also use select as below
h["incidents"].select {|h1| h1['key']=='xyz098'}.first['number']
Do as below
# to get numbers from `'key'`.
json_hash["incidents"].map { |h| h['key'][/\d+/].to_i }
json_hash["incidents"] - will give you the value of the key "incidents", which is nothing but an array of hash.
map to iterate thorough each hash and collect the value of 'key'. Then applying Hash#[] to each inner hash of the array, to get the value of "key". Then calling str[regexp], to get only the number strings like '098' from "xyz098", finally applying to_i to get the actual integer from it.
If the given hash actually a json string, then first parse it using JSON::parse to convert it to a hash.Then do iterate as I said above.
require 'json'
json_hash = JSON.parse(json_string)
# to get values from the key `"number"`.
json_hash["incidents"].map { |h| h['number'] } # => [1, 2, 3]
# to search and get all the numbers for a particular key match and take the first
json_hash["incidents"].select { |h| h['key'] == 'abc123' }.first['number'] # => 1
# or to search and get only the first number for a particular key match
json_hash["incidents"].find { |h| h['key'] == 'abc123' }['number'] # => 1

Resources