Check if the index exists or not Elasticsearch - php

I want to check in elasticsearch if the index exists or not. If it not exists it should create the index and do other functionality. I try to find out a solution for that, but did not find any perfect solution for that. Can anyone have any solution to solve this problem.
I am using Elasticsearch library.
**$client = new Elasticsearch\Client();**

As per index operations and source code the following should work
$client = new Elasticsearch\Client();
$indexParams['index'] = 'my_index';
$client->indices()->exists($indexParams);

This will return true or false:
$params = ['index' => 'products'];
$bool=$client->indices()->exists($params);

The documentation for list all indexes here: https://www.elastic.co/guide/en/elasticsearch/reference/current/_list_all_indexes.html
Using curl:
curl 'localhost:9200/_cat/indices?v'

Other way by using Facade:
use ScoutElastic\Facades\ElasticClient;
$indexParams['index'] = "model_index";
$exists = ElasticClient::indices()->exists($indexParams);
if ($exists) {
//do somthing
}

For more recent versions of Elasticsearch (8.x), using the php library (with corresponding version 8.x) a call to $client->indices()->exists($indexParams) no longer returns a boolean, it instead returns an instance of Elastic\Elasticsearch\Response\Elasticsearch. This response is actually a 200 HTTP response if the index exists, or a HTTP 404 if it does not, but it also has the helpful asBool() you can use that abstracts away the HTTP codes e.g.
$client->indices()->exists($indexParams)->asBool();

I was able to do it with node.js, as below
const { Client } = require('#elastic/elasticsearch');
const client = new Client({
node: ES_URL,
});
await client.indices.exists({ index: 'INDEX_NAME' });
and the response should be similar to this one below:
{
body: true,
statusCode: 200,
headers: {
date: 'Sun, 07 Mar 2021 13:07:31 GMT',
server: 'Apache/2.4.46 (Unix) OpenSSL/1.1.1d',
'content-type': 'application/json; charset=UTF-8',
'content-length': '2796',
'keep-alive': 'timeout=5, max=100',
connection: 'Keep-Alive'
},
meta: {
context: null,
request: { params: [Object], options: {}, id: 1 },
name: 'elasticsearch-js',
connection: {
url: 'ES_URL',
id: 'ES_ID',
headers: {},
deadCount: 0,
resurrectTimeout: 0,
_openRequests: 0,
status: 'alive',
roles: [Object]
},
attempts: 0,
aborted: false
}
}

Related

Symfony 5 - Failed to start the session because headers have already been sent by "php://input" at line 1 in production

*** EDIT - SOLVED ***
In case it may help someone one day, the real cause was due to a malware on my server which was mining crypto through my php-fpm container.
If you want to check :
Install htop to your server if it is ot already installed
Look for you CPU utilisation if there is a program using all of it
Remove it
Secure your docker with networks and don't open port 9000
In my case, it was this one "devtmpfsi"
kdevtmpfsi using the entire CPU
*** END OF EDIT ***
I know this error is old like the world but after searching for a solution for about three days on every different websites and topics I couldn't find anything that match my case.
So I hope so someone might help me.
Context :
I'm using the function fetch in JS to get my data
I'm using docker in dev and prod environnement with nginx (1.10.3) and php-fpm (PHP: 7.4.12)
Everything work fine in dev environnement
It happen about few minutes / or after multiple refresh of the page, but everything work when I restart docker, and then, happen again
Screenshots of the error
First Screenshot
Second screenshot
Code
JS :
const fetchSkinWithParameters = () => {
fetch(platformUrl + '/api/skins', {
method: "POST",
body: JSON.stringify({
currentIndex: containerPage1.dataset.currentindex,
type: containerPage1.dataset.currenttype,
})
}).then((res) => {
return res.json();
}).then((data) => {
data = data.data;
name = data.name;
totalSkin = data.totalSkin;
id = data.id;
skinSelectorPreview.dataset.id = id.toString();
skinSelectorPreview.setAttribute('src', '/assets/images/skins/' + name);
});
}
PHP (symfony 5):
/**
* #Route("/api/skins"), name="choose_skin"
*/
public function chooseSkin(Request $request)
{
$message = json_decode($request->getContent(), true);
$type = $message['type'];
$em = $this->getDoctrine();
$skins = $em->getRepository(Skin::class)->findByType($type);
$currentIndex = $message['currentIndex'];
$data = [
'id' => $skins[$currentIndex]->getId(),
'name' => $skins[$currentIndex]->getName(),
'totalSkin' => count($skins),
];
$response = new Response();
$response->setContent(json_encode([
'data' => $data,
]));
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
return $response;
}
Solutions I tried :
Modifying my php.ini (output_buffering)
Checking if I didn't have stupid spaces before any <?php
And many little thing about session (starting a new one an so on)
This is my first in here post since I always found solutions over here, I'm quite annoyed right now :/
I'll provide any informations that you'll need / or I forgot to put in here if you ask
Thanks for your time.
On your JS add headers this way:
fetch(platformUrl + '/api/skins', {
method: "POST",
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
currentIndex: containerPage1.dataset.currentindex,
type: containerPage1.dataset.currenttype,
})
})

How to solve this 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control in vueJS?

Access to XMLHttpRequest at 'http://localhost/webserivcedemo/state.php' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The Access-Control-Allow-Origin header contains multiple values *, *,but only one is allowed.
I use $http for call webservice.
This is my request code.
export default {
data() {
return {
name: "",
number: "",
email: "",
submitdata: false
};
},
methods: {
handleSubmit: function() {
var data1 = {
title: this.name,
body: this.number + this.email
};
this.$http
.post("http://localhost/webserivcedemo/state.php", {
data: data1
})
.then(function(data) {
console.log(data);
this.submitdata = true;
});
}
}
};
you need to create new file vue.config.js and need to add the urls that are needed to access as below.
module.exports = {
devServer: {
proxy: 'endpoint',
}
}
solution 1: please check 1st same port/server like as http://127.0.0.1:8080 running twist or not?
If twist then close one. then check.
solution 2: sometimes problem for extra / API endpoint. remove the extra / then check.
thank you

Angular $http always returning null data from PHP API

I am trying to use a login API that one of my developers created for me.
$http({
method: 'POST',
url: "http://example.com/api/userLogin",
data: $.param(postLoginData),
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
responseType: 'json'
}).then(function(loginData) {
console.log(loginData);
});
My console.log() always logs the following:
Object {data: null, status: 200, config: Object, statusText: "OK"}
Yet when I go to the Network tab within developer tools, I can see that the response is actually the following:
I'm not a backend developer, so I was a little confused with this 0 at the beginning of the response. I then tried to investigate this further and looked into the PHP API code itself, and found that the response from curl_exec($loginCurl) that is returned is:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Mon, 09 May 2016 02:10:35 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Cache-Control: no-cache
Content-Length: 43
Content-Type: application/json
{"id":"16","username":"user4","status":200}
The body of the response is valid JSON, so not too sure why Angular is returning null data, even though I can see the response in developer tools successfully...
EDIT:
My API contained the following to perform checks and create sessions:
$jsonResults = json_decode($body, true);
if($jsonResults != NULL || $jsonResults->id != NULL) {
//Successfully logged in
$_SESSION['user_name'] = $jsonResults->username;
$_SESSION['user_login_status'] = 1;
$_SESSION['user_id'] = $jsonResults->id;
$this->response($body, 200);
}
Although, if the entire above code is just replaced with the following, it seems to work perfectly fine:
$this->response($body, 200);
Why would this be?
The problem is definitely server-side however you can work around it using a response transformer. For example...
$http.post('http://example.com/api/userLogin', $.param(postLoginData), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
transformResponse: function(data) {
console.log('Raw data', data);
if (angular.isString(data) && data[0] === '0') {
data = data.substring(1);
}
return angular.fromJson(data);
}
}).then(function(response) {
console.log(response);
})

ExtJs4 Trouble with null JSON server side on store.sync

here is my store code:
var sql = Ext.create('Ext.data.Store', {
model: 'SQL',
groupField: 'project',
proxy: {
type:'ajax',
api: {
read: 'data/showText.php', // Called when reading existing records
update: 'data/saveRollout.php' // Called when updating existing records
},
actionMethods: {
read : 'GET',
update : 'POST'
},
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json'
}
},
autoSync: true,
autoLoad: true
});
It sends right json code
{
"projectId":102,
"project":"2G Rollout and Performance",
"taskId":123,
"description":"2)Capacity Sites Delta",
"january":123,
"february":0,
"march":0,
"april":0,
"may":0,
"june":0,
"july":0,
"august":0,
"september":0,
"october":0,
"november":0,
"december":0,
"id":null
}
But there is NULL on response in php file
var_dump(json_decode($_POST, true));// shows NULL on response
You are actually sending your data to the server via the request body, not via the url.
Using:
$iRequestBody = file_get_contents('php://input');
Will work.
See similar issue and some (additional) excellent answers.
Have you tried taking out:
writer: {
type: 'json'
}
To see if it handles regular text sent to the server, maybe your server is not handling the JSON encoding well.

CodeIgniter REST API Library Ajax PUT throwing 403 Forbidden

I got the rest of the library working fully, just trying to generate api keys and its throwing a 403 forbidden when executed via ajax.
({"status":false,"error":"Invalid API Key."})
I traced it to _remap function under REST_Controller.. almost as if im calling the url incorrectly?
workflow: user visits site1.com -> registers for account -> generates api key for their domain -> key recorded in db -> key displayed
The following form would be on site1.com after they register for an account they would click "generate key".
ajax call:
/**
* Generate an API Key for Us to use
*/
$("#submitGetApiKey").click(function(){
$.ajax({
url: "http://dev.site1.com/api/key",
crossDomain: true,
type: "PUT",
dataType: "jsonp",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
for (var i = keys.length - 1; i >= 0; i--) {
console.log(keys[i]);
};
}
});
});
REST-SERVER on GitHub: https://github.com/philsturgeon/codeigniter-restserver
look specifically at key.php under application/controllers/api/key.php
Snippet of the key.php file that should relate to this process:
/**
* Key Create
*
* Insert a key into the database.
*
* #access public
* #return void
*/
public function index_put()
{
// Build a new key
$key = self::_generate_key();
// If no key level provided, give them a rubbish one
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
// Insert the new key
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
{
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
}
else
{
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
}
}
Response/Request Headers
Request URL:http://dev.mapitusa.com/api/key
Request Method:PUT
Status Code:403 Forbidden
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e165df34aa4fda5936e940658030f83d%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328291821%3B%7Dac0f163b112dbd3769e67f4bb7122db2
Host:dev.mapitusa.com
Origin:http://dev.mapitusa.com
Referer:http://dev.mapitusa.com/api_test.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.3 Safari/535.19
Response Headersview source
Cache-Control:max-age=0, public
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:69
Content-Type:application/json
Date:Fri, 03 Feb 2012 18:03:54 GMT
Expires:Fri, 03 Feb 2012 18:03:54 GMT
Keep-Alive:timeout=5, max=98
Server:Apache
Set-Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22f2f466f7b97b89f2a9b557d2d9a0dbcc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328292234%3B%7D6821b96c7e58b55f1767eb265ffdb79e; expires=Fri, 03-Feb-2012 20:03:54 GMT; path=/
Status:403
Vary:Accept-Encoding,User-Agent
X-Powered-By:PHP/5.3.6
X-UA-Compatible:IE=Edge,chrome=1
i ended up finding out the 403 forbidden was because i was not providing an api key to generate keys..
Kind of abiguous as Phil's documentation doesn't state that an existing api key is required before you can generate keys..
I simply created a bogus key in the table in the db and referenced that when calling /key/index?X-API-KEY=boguskey
I have solved the problem of generating the api key.
I'm using Phil Sturgeon's REST API server.
Call the key controller using ajax call as such :
$("#submitGetApiKey").click(function(){
$.ajax({
url: "http://sitename.com/api/key/index?X-API-KEY=your_key_here",
crossDomain: true, /* remove this if using the same domain*/
type: "PUT",
dataType: "jsonp",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
for (var i = keys.length - 1; i >= 0; i--) {
console.log(keys[i]);
};
}
});
});
Inside key controller:
Search for function _generate_key() and check for $this->load->helper('security');. the security helper must be loaded for working of do_hash otherwise you will get 500 internal server error.
public function index_put()
{
// Build a new key
$key = self::_generate_key();
// If no key level provided, give them a rubbish one
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
// Insert the new key
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
{
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
}
else
{
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
}
}
Also, you may call http://sitename.com/api/keyindex?X-API-KEY=your_key_here in your browser's address bar by making a small change in your key controller
you can replace the function name index_put with index_get.
Thanks
If you are calling this from a different domain, you may be running into some XSS issues. You might have to run it from your own server and call the function from it's own domain or possibly use the JSONP capability.
UPDATE: Are you able to see the transaction in Firebug using the NET Tab?
Do you get JSON Back?
Sometimes you have to add callback=? to the url request:
http://dev.site1.com/api/key?callback=?
Update2: Are you able to bring the page up in the browser: (http://dev.mapitusa.com/api/key)
If you get the same error, you should try giving 777 (full read/write) permissions to the site.
This sounds like it might be a browser issue. Maybe an incorrect implementation of PUT in the XMLHttpRequest stack.
I would try converting it quickly to POST just to see if it works. It might be better off leaving it as POST anyway just for compatibility purposes.

Categories