ElasticSearch POST with json search body vs GET with json in url - search

According to the ES documentation, those 2 search request should get the same results:
GET
http://localhost:9200/app/users/_search?source={"query": {"term": {"email":"foo#gmail.com"}}}
POST
http://localhost:9200/app/users/_search
Post body :
{
"query": {
"term": {
"email":"foo#gmail.com"
}
}
}
But the first one gives no result while the second one gives me the expected result. I use ES version 0.19.10
Did anybody else have the same behavior ? Is this a bug ?

source is not a valid query string argument according to URI Search
Elasticsearch allows three ways to perform a search request...
GET with request body:
curl -XGET "http://localhost:9200/app/users/_search" -d '{
"query": {
"term": {
"email": "foo#gmail.com"
}
}
}'
POST with request body:
Since not all clients support GET with body, POST is allowed as well.
curl -XPOST "http://localhost:9200/app/users/_search" -d '{
"query": {
"term": {
"email": "foo#gmail.com"
}
}
}'
GET without request body:
curl -XGET "http://localhost:9200/app/users/_search?q=email:foo#gmail.com"
or (if you want to manually URL encode your query string)
curl -XGET "http://localhost:9200/app/users/_search?q=email%3Afoo%40gmail.com"

You should URL encode your query in the first case:
http://localhost:9200/app/users/_search?source=%7b%22query%22%3a+%7b%22term%22%3a+%7b%22email%22%3a%22foo%40gmail.com%22%7d%7d%7d

The above answers are not compatible with the Elastic search Version 6.0 and above.The ES introduced Strict Content type check from the version 6.0.
The link explains it:
https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests.
For curl, need to add -H'Content-Type: application/json' to the command line of any request that has a JSON body

Related

Updating field from Firestore UI removes line breaks

I have the following two routes that take care of adding and getting a person on Firestore:
app.post('/api/person/add', (req, res) => {
const person = {
name: 'Bill Gates',
email: 'bill.gates#gmail.com',
description: "City: Redmond\nState: WA",
};
admin.firestore().collection('persons').doc(`/${req.body.id}/`).create(person);
res.status(200).json({ status: 'success' });
});
app.get('/api/person/get/:id', async (req, res) => {
const item = await admin.firestore().collection('persons').doc(req.params.id).get();
const person = item.data();
res.status(200).json(person);
});
If I run the following command for adding a person:
$ curl -s --request POST 'http://localhost:5001/happy-fox/us-central1/app/api/person/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": 1,
"name": "Bill Gates",
"email": "bill.gates#gmail.com",
"description": "City: Redmond\nState: WA"
}'
I get:
{"status":"success"}
If I run the following command for getting a person:
$ curl -s --request GET 'http://localhost:5001/happy-fox/us-central1/app/api/person/get/1'
I get:
{"description":"City: Redmond\nState: WA","email":"bill.gates#gmail.com","name":"Bill Gates"}
Please notice the line break between the city name: Redmond and the word: State.
On my use case I need to modify the State manually (WA → NY) through the Firestore Panel UI, by doing the following:
My problem is: after updating that field, when I run the GET command again:
$ curl -s --request GET 'http://localhost:5001/happy-fox/us-central1/app/api/person/get/1'
I get:
{"description":"City: RedmondState: NY","email":"bill.gates#gmail.com","name":"Bill Gates"}
where you can see that the line break between the city name: Redmond and the word: State disappeared.
Is there any way I can do the update through the Firestore Panel UI without losing the line breaks?
Thanks!
Currently, Firestore does that to preserve the UI integrity. However, there's a workaround for that specific issue.
Manually put \n to the string when editing in Firestore UI. e.g:
"Redmond\nState: NY". By doing this, If you get/fetch the data, you'll be
getting Redmond\\nState instead. So you need to create a parser to
modify all \\n to n. sample code below:
var string = "Redmond\\nState: NY"
string = string.replace("\\n", "\n");
I would also suggest creating a Feature request for this issue.

How to restrict _rev_info in cloudant result json

I am using cloudant for my Project. Every time i update a document and fetch a document, the result JSON comes with { _rev_info : [...] } (contains 500+ rev history). how i restrict & fetch data without _rev_info in cloudant??
{ name: "test",
"age":22,
_revs_info: [
{ rev: '510-454.....',
status: 'available' },
{....}
]
}
_revs_info is only be returned if you explicitly request it by passing revs_info=true in the query string. If you don't require the revision history, just exclude that parameter.

Elasticsearch - How to find out what is the type of a particular index

I created a document in ES. Later I want to query it, but I forget the type, therefore I can not complete the command. However I still remember the the index name.
How do I retrieve the type name of the index?
If you know the index name, you can quickly retrieve all the mapping types in your index using the following command:
curl -XGET 'http://localhost:9200/index_name/_mapping?pretty'
You'll get a response like the one below:
{
"index_name": {
"mappings": {
"type_name": { <----- this is the type name you're looking for
"properties": {
...field definitions...
}
}
}
}
}

CouchDB not recognising the update functions in my new design document

I have create a new design documents with a dummy update function. But when I try to test it, CouchDB is not identifying my function in the design document.
Below is my design document:
{
"_id": "_design/payable_draft",
"_rev": "13-c9c9a9f88c24b75cdd28204a526f66a6",
"updates": "{\"empty_update\":\"function(doc, req){\n\treturn [doc,toJSON(\"empty_update\")];\n}\"}"
}
But when I try to invoke this update function using a put, getting 404.
Request:
PUT /db/_design/payable_draft/_update/empty_update/my_doc HTTP/1.1
Host: <my couchDB>
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache
Response:
{
"error": "not_found",
"reason": "missing updates function empty_update on design doc _design/payable_draft"
}
I see everything looks fine, not able to find the issue. Any help would be greatly appreciated.
I think your escaping is wrong. Your "updates" block contains one complete string. Try this one:
{
"_id":"_design/payable_draft",
"_rev":"13-c9c9a9f88c24b75cdd28204a526f66a6",
"updates":{
"empty_update" : "function(doc, req){return [doc,toJSON('empty_update')];}"
}
}
See http://wiki.apache.org/couchdb/Document_Update_Handlers for more details. As described there one must place the update handler as attributes below "updates":
"updates" : {
"myhandler" : "function(doc, req) { ... }"
}

Node.js - Send and receive Array as GET/POST using querystring

I've got the following code, but it doesn't seem to work:
var post_req = {
array: [
[ {
param1: 'something',
param2: 123
} ],
[ ],
[ ],
[ {
param2: 'something',
param4: 1234,
param1: 'hello'
} ]
]
};
var data_send = querystring.stringify(post_req);
var request = client.request('POST', '/', headers);
request.end(data_send);
and
if( req.method == 'POST' ) {
req.addListener('data', function(chunk)
{
POST = querystring.parse(chunk);
console.log(POST);
}
}
I end up with 5 sub-arrays, corresponding to the 5 parameters in the objects but with extra '][' characters in their names:
{ array:
[ { '][param1': 'something' }
, { '][param2': '123' }
, { '][param2': 'something' }
, { '][param4': '1234' }
, { '][param1': 'hello' }
]
}
There is a new node package that fixes this: "npm install qs".
https://github.com/ljharb/qs
"query string parser for node supporting nesting, as it was removed from 0.3.x, so this library provides the previous and commonly desired behaviour (and twice as fast)"
If anyone can tell me why it was removed from 0.3.x, I will give you an upvote for your comment. (I want my confidence in Node.js restored.)
To confirm my comment above, node's querystring.stringify function won't handle nested arrays (at the time of writing).
You can see the source of stringify at https://github.com/ry/node/blob/master/lib/querystring.js
Note that it handles one level of arrays but it doesn't recurse. When it finds an array it uses stringifyPrimitive to encode the array's values. You can see that stringifyPrimitive doesn't handle arrays, only number, boolean and string.
As I suggested in my comment, given that you're using a POST request a better idea would be to use JSON encoding for your complex data structure.
Or use https://github.com/visionmedia/node-querystring as suggested by #FriendlyDev
Don't use querystring.parse for recreating a JSON object sent as string. Use instead JSON.parse. And use JSON.stringify instead of querystring.stringify
querystring is useful when you send parameter encoded in the url or when you post a form. But there is no point in using it if you send just one JSON object with all your data.
In your scenario I would dismiss the querystring library and use JSON library, which is already included. It would be cleaner and faster.
http://www.json.org/js.html

Resources