Mysql Between query in elasticsearch - php

Currently I am Working on Elastic Search For My Current Project. I need MySql BETWEEN in my elastic. I searched So Many Examples But Not Get Any Solution I need to sort using no. of employees by Company. I already Wrote Some Filters Its Working Fine. But I Need Range Filter. and my code as follows
$query = array("from" => $start,
"size" => $recordslimit,
"sort" => array(array('id' => 'desc')),
"query" => array(
"filtered" => array(
"query" => array("match_all" => array()),
"query" => (!empty($keyword)) ? array("match" => array('_all' => $keyword)) : null,
"filter" => array(
"bool" => array(
'should' => array(
array("match" => array('location' => $headerLocation))
),
'must' => array(
($type == '1') ? array('term' => array('user_sub_type' => '1')) : null,
array('term' => array('user_type' => 'v')),
array('term' => array('status' => 'a'))
),
'must_not' => array(
array('term' => array('subscription_type' => 'n'))
))
)
))
);
}
}
$data = $this->elasticsearch->advancedquery("users",json_encode($query));
this is current query.. and also i add in filter
"range" => array(
'num_of_employees' => array(
'gt' => 100,
'lte' => 1000
)
)
please helpout with this situation. Thanks in Advanced.

Related

How to convert mongoDB query into PHP?

My query is working in mongodb and produce output Properly. But same query i tried to run using php then it gives following error
Array
(
[ok] => 0
[errmsg] => A pipeline stage specification object must contain exactly one field.
[code] => 16435
)
Following is my MongoDB Query
db.sample_coll.aggregate(
{
$unwind: {
path:"$KeyValues",
includeArrayIndex:"arrayIndex",
preserveNullAndEmptyArrays:true
}
},
{
$project: {
timestamp:{
"$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}]
} ,
"InputVolt":"$KeyValues.InputVolt",
arrayIndex:1
}
},
{
$match: {
$and: [
{InputVolt: {$ne: null}}
]
}
}
);
The Above query is converted in php
$pipeline = array(
array('$unwind' =>
array( 'path' => '$KeyValues'),
array( 'includeArrayIndex' => 'arrayIndex' ),
array( 'preserveNullAndEmptyArrays' => 'true' )
),
array(
'$project' => array(
'timestamp' => array(
'$add' => array(
'$EventTS',
array('$multiply' => array( 60000, '$arrayIndex' ))
)
),
array( 'InputVolt' => array( '$KeyValues', 'InputVolt' ) ) ,
array('arrayIndex' => 1)
)
),
array(
'$match' => array(
'$and' => array(
array('InputVolt' => array('$ne' => null )),
)
)
)
);
$result = $collection->aggregate($pipeline);
Kindly help to resolve this issue.
Thanks in advance
Your pipeline in PHP:
$pipeline = array(
array('$unwind' => array(
'path' => '$KeyValues',
'includeArrayIndex' => 'arrayIndex',
'preserveNullAndEmptyArrays' => true
)),
array('$project' => array(
'timestamp' => array(
'$add' => array('$EventTS', array('$multiply' => array(60000, '$arrayIndex')))
),
'InputVolt' => '$KeyValues.InputVolt',
'arrayIndex' => 1
)),
array('$match' => array(
'InputVolt' => array('$ne' => null)
))
);

MongoDB find document in collection based on 2 IDs PHP

I was wondering how to find document in collection based on UIDa and UIDb which could be also vice versa.
So for example I want to get all documents where
value UIDa = inputA and UIDb = inputB
or value UIDa = inputB and UIDb = inputA
{
"_id" : ObjectId("5572f65dc89782b9398b4fc0"),
"sender" : "senderID",
"recipient" : "recipientID",
"message" : "messagehere",
"time" : "1433092679",
"state" : "1"
}
I have this query but it doesn't work because some of the documents are missing.
$query = $db->collection->find(array(
'$or' => array(
array(
'recipient' => $uid,
'sender' => $fuid,
'state' => '1',
'time' => array(
'$lt' => $last_update
)
),
array(
'recipient' => $fuid,
'sender' => $uid,
'state' => '1',
'time' => array(
'$lt' => $last_update
)
)
)
)
)->limit(20);
thanks in advance.
Well I think what you are looking for the $in operator.
$query = $db->collection->find(array(
'recepient' => array('$in' => array($uid, $fuid)),
'sender' => array('$in' => array($uid, $fuid)),
'state' => '1',
'time' => array('$lt' => $last_update)
)
)->limit(20);

Elasticsearch range filter not working (No filter registered for)

I'm trying ta add a range filter to my search (only articles with workflow status <= 5 should appear) and getting errors
what's wrong with my range?
$searchParams = array(
'index' => $config->search->index,
'type' => 'xxxxxxxxxx',
'size' => 10,
'from' => ($page - 1) * 10,
'body' => array(
'query' => array(
'filtered' => array(
'query' => array(
'multi_match' => array(
'query' => $query,
'fields' => array('title^8', 'caption^5', 'teasertext^4', 'articletext^2') // ^x = Gewichtung des Felds
),
),
'filter' => array(
'bool' => array(
'must' => array(
'term' => array(
'deleted' => 0,
'visible' => 1
),
'range' => array(
'workflow_status' => array('lte' => '5')
)
)
)
)
)
)
),
'sort' => array('_score', '_id:desc')
here my mapping for status:
"workflow_status": {
"type": "integer",
"index": "not_analyzed"
}
What you have is not a valid Query DSL , must should take in an array.
The body should be something on these lines :
body =>
array(
'query' => array(
'filtered' => array(
'query' => array(
'multi_match' => array(
'query' => $query,
'fields' => array('title^8', 'caption^5', 'teasertext^4', 'articletext^2') // ^x = Gewichtung des Felds
),
),
'filter' => array(
'bool' => array(
'must' => array(
array(
'term' => array(
'deleted' => 0
)
),
array('term' => array(
'visible' => 1
)
),
array(
'range' => array(
'workflow_status' => array('lte' => '5')
)
)
)

Lithium framework multiple 'or' statements in a query (using mongoDB)

I have started work with lithium framework + mongoDB recently. I want to do a really simple query which contains multiple or statements.
I have articles in the DB with publish_up and publish_down fields. I want to fetch only those records/documents which pulbis_down field is highert than now OR null AND publish_up field lower than now OR null.
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
'$or' => array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
),
)
));
Of course this snippet is wrong hence the second or statement overwrites the first one (because the same array key).
I tried to wrap them into an individual array but gives error.
Any idea?
This query will fetch articles with (publish_down > now OR publish_down = null) AND (publish_up < now OR publish_up = null)
$items = Articles::find('all', array(
'conditions' => array(
'$or' => array(
array('publish_down' => array('$gt' => $mongoDateNow)),
array('publish_down' => null)
),
'$or' => array(
array('publish_up' => array('$lt' => $mongoDateNow)),
array('publish_up' => null)
),
)
));
I don't know about lithium but in PHP you can use multiple $OR as below
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
)
),
)
));
I think the correct answer is:
'$and' => array(
array(
'$or'=>array(
array('publish_up' => null),
array('publish_up' => array('$lt' => $mongoDateNow))
)
),
array(
'$or'=>array(
array('publish_down' => null),
array('publish_down' => array('$gt' => $mongoDateNow))
)
)
)

TYPO3 suggest wizard, search in substring

I use the suggest wizard in TYPO3 backend.
The following code is in the tca:
'tx_db_colors' => array (
'exclude' => 0,
'label' => 'Farbe',
'config' => array (
"type" => "group",
"allowed" => "tx_db_colors",
"foreign_table" => "tx_db_colors",
"internal_type" => "db",
"size" => 1,
"minitems" => 0,
"maxitems" => 1,
'items' => array(array('', ''),),
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
),
),
)
),
Is there a solution, to get matched records in substring of the label, not from scratch?
Example:
The records label is named 'coffee black'
When I type 'co' into the search field, the record will be shown.
'blac' won't match to any record.
Is this possible to find this record, when I type in a substring? Else I have to extend the autocompletion. TYPO3 Core, yuk! :-)
Thank you in advance!
After hours, I found the solution.
You have to write the tca like this:
'tx_db_colors' => array (
'exclude' => 0,
'label' => 'Farbe',
'config' => array (
"type" => "group",
"allowed" => "tx_db_colors",
"foreign_table" => "tx_db_colors",
"internal_type" => "db",
"size" => 1,
"minitems" => 0,
"maxitems" => 1,
'items' => array(array('', ''),),
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
'default' => array(
'searchWholePhrase' => 1
),
),
),
)
),
Just add
'default' => array(
'searchWholePhrase' => 1
),
into the 'suggest' array.

Categories