Elasticsearch URI Search multiple fields - elasticsearch

I can do a quick URI search like
GET twitter/tweet/_search?q=user:kimchy
Can I search multiple fields this way? For example, user:kimchy AND age:23?
What I tried 1 (error):
curl -XDELETE localhost:9200/myindex/
curl localhost:9200/myindex/mytype/1 -d '{"a":1,"b":9}'
curl localhost:9200/myindex/mytype/2 -d '{"a":9,"b":9}'
curl localhost:9200/myindex/mytype/3 -d '{"a":9,"b":1}'
Say I want just the document {"a":9, "b":9}, I tried
GET localhost:9200/myindex/_search?q=a:9&b:9
but I get error
{
error: {
root_cause: [{
type: "illegal_argument_exception",
reason: "request [/myindex/_search] contains unrecognized parameter: [b:9]"
}],
type: "illegal_argument_exception",
reason: "request [/myindex/_search] contains unrecognized parameter: [b:9]"
},
status: 400
}
What I tried 2 (works!):
GET localhost:9200/myindex/_search?q=a:9 AND b:9
The spaces are important. Alternatively, use %20.

Yes, you can. Try something like this:
GET twitter/tweet/_search?q=user:kimchy%20AND%20age:23
Note that if you URI decode this, it's equivalent to:
GET twitter/tweet/_search?q=user:kimchy AND age:23
Note that when you are using this REST endpoint like this, I think you are really taking advantage of something like the query_string_query. Refer to those docs to get an idea of the extent of the query string language and features available to you.

Related

Kibana visualize use wild card in search bar

Is it possible to use wild card in Kibana visualize search bar.
Tried to use it like below, but did not work.
operation: "Revers" NOT file:"*Test.Revers"
This returns 2 because there are two Revers terms ("Revers", "/test/count/Test.Revers" ) even though only one data entry is in the stats data.
The following also returns the same value as 2.
operation: "Revers"
Stat data sample is as below.
"_source": {
"status": 0,
"trstime": 1819,
"username": "test",
"operation": "Revers",
"file": "/test/count/Test.Revers"
}
I have tested it in ES 7.10 as you not mentioned ES version.
Answer to your question is YES, you can use wildcrad in Kibana visualize search bar but value should be without double quotes. Because if you give value in doble quotes it will consider as text and search for it.
You can try below query and it will give you your expected output:
operation: Revers AND NOT file.keyword: *Test.Revers
The result given for the below query as 1 without double quotes.
operation: Revers AND NOT file: *Test.Revers

Get Google Drive item id of file with known path

A file is located in a known path on Google Drive, for example:
/root/Myfiles/test.txt
How can I get the item-id of the file using the Google Drive V3 REST API (https://www.googleapis.com/drive/v3/files/)? In detail, I am not sure how to construct the query paramer q= for this.
Regards,
Unless you have the file id of MyFiles then your going to have to do this in two calls.
The first thing we will do is list all the directories in root.
This can be done using the Q parameter as you already know
By passing parents in 'root' and mimeType = 'application/vnd.google-apps.folder' and name ='Myfiles' I tell it that I am looking for a folder called Myfiles that has a parent folder of root.
curl \
'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%27root%27%20and%20mimeType%20%3D%20%27application%2Fvnd.google-apps.folder%27%20and%20name%20%3D%27YouTube%27&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
The response from this will then look something like this
{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "1R_QjyKyvET838G6loFSRu27C-3ASMJJa",
"name": "Myfiles",
"mimeType": "application/vnd.google-apps.folder"
}
]
}
I know know the file id of the folder called Myfiles
Now i can do another call which i request a file within that directory id with the name of test.txt like this parents in '1R_QjyKyvET838G6loFSRu27C-3ASMJJa' and name = 'test.txt'
The code will then look something like this
curl \
'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%271R_QjyKyvET838G6loFSRu27C-3ASMJJa%27%20and%20name%20%3D%20%27test.txt%27&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
The response
{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "1_BgrWKsjnZvayvr2kbdHzSzE3K2tNsWhntBsQwfrDOw",
"name": "test.txt",
"mimeType": "application/vnd.google-apps.document"
}
]
}
Summary
As #DalmTo said If you want to search for files within a specific folder you need to have that ID to search within it.
parents in Whether the parent’s collection contains the specified ID.
Which means that you should do two separate queries. One asking for the id of your folder and another looking for the file test.txt in that folder.
q: parents in "root" and mimeType = "application/vnd.google-apps.folder" and name = "Myfiles"
q: parents in "ID_FOLDER" and mimeType = "text/plain" and name = "test"
Example:
If you only have one file in your entire Drive that meets the required characteristics, you could do it in a single query:
q: name = "test" and mimeType = "text/plain"
Caution: If you have uploaded the file, Drive may have detected it as: application/octet-stream. Normally .txt files are detected as plain/text, for more information on MIME types and Drive API, you can check here for common MIME types and here for Drive specific types.
Alternative using Google Apps Script
Here is an example using Google Apps Script:
function findFile() {
var folderId;
var folderQuery = '"root" in parents and title = "Myfiles" and mimeType = "application/vnd.google-apps.folder"'
let folder = Drive.Files.list({
q: folderQuery
})
folderId = folder.items[0].id
let fileQuery = `parents in "${folderId}" and title = "test"`
var file = Drive.Files.list({
q: fileQuery
})
return file.items[0].id
}
Caution: Google Apps Script uses Drive API v2, in this case the query_term name becomes title
More Information
For a deeper understanding of how the Drive API works you can check Search for files guide:
A query string contains the following three parts:
query_term operator values
query_term is the query term or field to search upon.
operator specifies the condition for the query term.
values are the specific values you want to use to filter your search results
To keep in mind when used outside of a client library:
Note: These examples use the unencoded q parameter, where name = 'hello' is encoded as name+%3d+%27hello%27. Client libraries handle this encoding automatically.

Trying to get a JSONPath array of all leaves on json object

New to Go. My first project is to compare a NodeJS proxy and a Go proxy for account number tokenization. I have been doing NodeJS for a few years and am very comfortable with it. My proxies will not know the format of any request or response from the target servers. But it does have configurations coming from Redis/MongoDB that is similar to JSONPath expression. These configurations can change things like the target server/path, query parameters, headers, request body and response body.
For NodeJS, I am using deepdash's paths function to get an array of all the leaves in a JSON object in JSONPath format. I am using this array and RegEx to find my matching paths that I need to process from any request or response body. So far, it looks like I will be using gjson for my JSONPath needs, but it does not have anything for the paths command I was using in deepdash.
Will I need to create a recursive function to build this JSONPath array myself, or does anyone know of a library that will produce something similar?
for example:
{
"response": {
"results": [
{
"acctNum": 1234,
"someData": "something"
},
{
"acctNum": 5678,
"someData": "something2"
}
]
}
}
I will get an array back in the format:
[
"response.results[0].acctNum",
"response.results[0].someData",
"response.results[1].acctNum",
"response.results[1].someData"
]
and I can then use my filter of response.results[*].acctNum which translates to response\.results\[.*\]\.acctNum in Regex to get my acctNum fields.
From this filtered array, I should be able to use gjson to get the actual value, process it and then set the new value (I am using lodash in NodeJS)
There are a number of JSONPath implementations in GoLang. And I cannot really give a recommendation in this respect.
However, I think all you need is this basic path: $..*
It should return in pretty much any implementation that is able to return pathes instead of values:
[
"$['response']",
"$['response']['results']",
"$['response']['results'][0]",
"$['response']['results'][1]",
"$['response']['results'][0]['acctNum']",
"$['response']['results'][0]['someData']",
"$['response']['results'][1]['acctNum']",
"$['response']['results'][1]['someData']"
]
If I understand correctly this should still work using your approach filtering using RegEx.
Go SONPath implementations:
http://github.com-PaesslerAG-jsonpath
http://github.com-bhmj-jsonslice
http://github.com-ohler55-ojg
http://github.com-oliveagle-jsonpath
http://github.com-spyzhov-ajson
http://github.com-vmware-labs-yaml-jsonpath

How do I specify the --form field in Ruby code (for an HTTP GET request)?

I know that in a curl command, there is an option to specify a --form like this:
-F 'ns=com.my-organization.canvas-app'
I want to know how I can convert this to Ruby code when making an HTTP GET request.
I've managed to figure out how to specify the --data field in Ruby code. So if the --data field of a curl command looks like this:
-d property1=value1 -d property2=value2
Then the data field in the corresponding Ruby code should look something like this:
data = {property1: "value1", property2: "value2"}
But now I'm trying to understand how I can convert this:
-F 'ns=com.my-organization.canvas-app'
into the corresponding Ruby code.
I'm also using HTTParty for the HTTP requests.
As of now, this is the code I have for the GET request:
form={ns: "com.my-organization.canvas-app"}
getResponse = HTTParty.get(base_url,:body => form.json, :headers => $header)
puts getResponse.body
As you can see, I specified the --form in a variable called "form" and used it as an argument to the HTTParty.get() call. Am I doing this correctly?

RethinkDB Map/Reduce examples

I running through the Map/Reduce examples in the RethinkDB docs. I have some documents that look like this:
{
"category": "Fiction" ,
"content": "This far, no further!" ,
"id": "0fc5339b-8139-4996-8979-88a0051195e3" ,
"title": "The line must be drawn here"
}
When I follow the examples using Data Explorer. e.g.
r.table('posts').map(lambda post: 1)
I get this error:
SyntaxError: missing ) after argument list
This is because the Data Explorer only allows JavaScript as an input. You need to switch to something like this to make it work:
r.table('posts').map(function(post){return 1})

Resources