I am trying to integrate search by date range with PHP and elastic search
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'from' =>0,
'size' => 50,
'body' => [
'query' => [
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
]
]
];
$filter_date=array();
$filter_date['range']['datecreatedsort']['gte']='2015-11-27';
$filter_date['range']['datecreatedsort']['lte']='2017-11-27';
$params1['body']['query']['filtered']['filter']=$filter_date;
$params1['body']['sort']['datecreatedsort']['order'] = 'desc';
try {
$results = $client->search($params1);
//print_r($results);
}
catch (Exception $e) {
$last = $client->transport->getLastConnection()->getLastRequestInfo();
$last['results']['error'] = [];
print_r($last);
}
When I am running above query I am getting following error
[query_string] malformed query, expected [END_OBJECT] but found
[FIELD_NAME]","line":1,"col":78},"status":400}
datecreatedsort filed mapping is date type is date and value in
elastic search db is "datecreatedsort":"2016-05-30T09:39:40.000Z"
please help where is the issue in elastic query.
It's the native elasticsearch request.
In PHP you need to create array query with the same structure.
GET /joborders/_search?pretty=true
{
"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2017-11-22 13:49:00",
"lte": "2017-11-22 23:50:00"
}
}
}
]
}
}
PS You need to pass date ranges in the same format as datecreatedsort in db ("2016-05-30T09:39:40.000Z")
try this:
{
"query": {
"filtered": {
"query": {
"query_string": {
"default_field": "description",
"query": $wildCardString
}
},
"filter": {
"range": {
"datecreatedsort": {
"gte": '2015-11-27',
"lte": '2017-11-27'
}
}
}
}
}
}
your code for the query will look something like this:
$query=array(
'filtered'=>array(
'query' => array(
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
),
'filter'=>$filter_date
)
);
Hi Got the solution its working fine
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'body' => [
'query' => [
'bool' => [
'filter' => [
'range' => [ 'date_modified' => ['gt'=>$duration,'lt'=>$today,'boost'=> '2.0'] ]
],
'must' => [
'match' => [ 'description' => $wildCardString ]
]
]
]
]
];
Thanks guys for help.
Related
here is the data
req[
{
"_id": "123",
"_ar_id": "111",
"_val": "10",
"ar_date": "2023-02-02"
},
{
"_id": "1234",
"_ar_id": null,
"_val": "0",
"ar_date": "2023-02-02"
}
]
$validator = Validator::make($req->all(), [
'req.*._id' => 'required',
'req.*._val' => 'required',
'req.*.ar_date' => 'required',
]);
if ($validator->fails()) {
return response()->json(['stat' => 0,'msg' => $validator->errors(), 'data' => "Please input required details" ]);
}
since i validate the request, how can i use it in where to ?
here's the code
$record = ArRecordCount::where('ar_list_id', $list_id)->where('ar_date', $ar_date)->first();
and if has record then it will update, if no record then delete
An API I'm using need this specific format when I try anything else I get a 200 but without it actually working
{
"things": [
{
"awdad": "awd",
"234": "234234",
"234234234": {
"name": "Jon",
},
"wadadw": {
"234234234": "awdawd",
}
}
]
}
what I tried to do is this
$data['things'] = [
'awdad' => 'awd',
'234' => '234234',
'234234234' => [
'name'=>'Jon',
],
'wadadw' => [
'234234234'=>'awdawd',
],
];
after JSON encoding it gave me this
{
"things": {
"awdad": "awd",
"234": "234234",
"234234234": {
"name": "Jon"
},
"wadadw": {
"234234234": "awdawd"
}
}
}
I have a such json:
{
"id":1,
"name":"some name",
"decorations":[
{
"id":1,
"options":[
{
"type":"color",
"label":"Color",
"items":[
{
"label":"Green shades",
"color_group":{
"shade":"green",
"name":"Green"
},
"colors":[
{
"rgb":"249,249,249",
"hex":"#FBFBFB"
},
{
"rgb":"249,249,250",
"hex":"#FBFBFF"
}
]
},
{
"label":"White shades",
"color_group":{
"shade":"white",
"name":"White"
},
"colors":[
{
"rgb":"255,255,255",
"hex":"#FFFFFF"
}
]
}
]
},
{
"type":"position",
"label":"Position",
"items":[
{
"label":"Front"
},
{
"label":"Back"
}
]
}
]
}
]
}
which I want to validate, rules are:
id, name, decorators required
each decorator should have id and options
each option should have type, label and items. each option has few types, all of them has same structure of each item except of a color type
so each item should have label, and in case of color option each item should have color_group, colors
- where color_group should have 'shade' and name
- each color in colors should have rgb and hex
I want to use symfony assertions to validate that json, and all is more or less straightforward except that rule where depends on a type I have different rules... I do not know how to do it right, I found that I can use expression or callback assertion but in both case it showing how to validate object but not a array.
Here is what I have:
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
'id' => [
new Assert\Required(),
new Assert\Type(['type' => 'integer']),
],
'decorations' => new Assert\All(
[
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
'id' => [
new Assert\Required(),
new Assert\Type(['type' => 'integer']),
],
'options' => new Assert\All(
[
'type' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
],
'label' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
],
'items' => new Assert\All(
[
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
// label is required for all types
'label' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
]
// here I should add constraints for colors, if type equal to "color", but do not know how to make that if/else statement...
]
]
)
]
)
]
)
]
]
)
]
)
]
]
);
I have the following function which searches for results in elasticsearch.
I want to do the following request with PHP and Guzzle.
/**
* {#inheritdoc}
*/
public function sendSearchRequest($es_node, $request) {
try {
if (isset($es_node)) {
$ssl = $es_node->get('field_ess_verify_ssl')->value;
$ssl_val = $ssl ? 'https://' : 'http://';
$body = [
'json' => [
'query' => [
'bool' => [
'should' => [
[
'multi_match' => [
'query' => $request->get('search'),
'fields' => ['message', 'event.type'],
'operator' => 'AND',
],
],
[
'match' => [
'event.type' => $request->get('type'),
],
],
[
'match' => [
'event.labels' => $request->get('label'),
],
],
],
],
],
],
];
$response = $this->httpClient->request('POST', $ssl_val . $es_node->get('field_ess_host')->value . ':' . $es_node->get('field_ess_port')->value . '/***/***/_search?pretty', $body)
->getBody()
->getContents();
return json_decode($response, TRUE)['hits']['hits'] ?? '';
}
} catch (Exception $exception) {
\Drupal::logger(__METHOD__)->error('No ES node has been found.');
return FALSE;
}
}
But with this i get a parsing exception which means that the multi_match doesn't work this way. If i use a 'normal' match in that place it's working fine but then i am limited to 1 field.
The other match fields uses other form fields for input and that has always one value.
But with form field 'Description search' i want to look in multiple fields with the AND operator.
Anyone knows how to fix this?
Screenshot of the form:
Your query looks good to me and ran in my test. It should work as is.
But in the interest of future readers, this is how multi-match queries work with elastic search.
GET /_search
{
"query": {
"multi_match" : {
"query": "Will Smith",
"type": "best_fields",
"fields": [ "first_name", "last_name" ],
"operator": "and"
}
}
}
Note that this example from the elastic search documentation is identical in nature to your section of the code above:
...
'multi_match' => [
'query' => $request->get('search'),
'fields' => ['message', 'event.type'],
'operator' => 'AND',
],
...
This is how multi-match queries are formed, and you did it.
i have a table commande witch contains a date, heure ... i want to return a table witch gives me a commande group by date but my problem i get one commande for each date wwhen i have more than a commande on that date her is my request:
$factures = $this->Commande->find('all', array(
'conditions' => array(
'Commande.prestataire_id' => $this->request->data['prestataire_id']
),
'group' => array('Commande.date'),
'order' => array('Commande.date' => 'DESC'),
'fields' => array(' Commande.date'),
));
here is the output:
{
"status": "Success",
"status_code": 200,
"message": "Commande trouvé",
"data": {
"everjob_get_Mission": {
"data": [
{
"Commande": {
"date": "2018-04-25"
}
},
{
"Commande": {
"date": "2018-04-24"
}
},
{
"Commande": {
"date": "2018-04-23"
}
},
{
"Commande": {
"date": "2018-04-19"
}
},
{
"Commande": {
"date": "2018-04-20"
}
}
]
}
}
}
Try to below code. I hope its work for you not sure.
$factures = $this->Commande->find('all', array(
'fields' => 'DISTINCT Commande.date',
'conditions' => array(
'Commande.prestataire_id' => $this->request->data['prestataire_id']
),
'group' => array('Commande.date'),
'order' => array('Commande.date' => 'DESC')
));