How to filter by category in Magento 2's API? - rest

In Magento 2's REST API, there is an option to search a product using various search criteria. As you know, an example is given below:
http://magentohost/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name& searchCriteria[filter_groups][0][filters][0][value]=%macbook%& searchCriteria[filter_groups][0][filters][0][condition_type]=like
But I have not found an option to search by category.
How can we do that?

To search by category, it is simple. You have to just pass category_id as a field. Have a look at below example:
http://magentohost/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id& searchCriteria[filterGroups][0][filters][0][value]=4& searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at& searchCriteria[sortOrders][0][direction]=DESC& searchCriteria[pageSize]=10& searchCriteria[currentPage]=1

You can also target multiple categories at once:
searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=1,2,3&searchCriteria[filter_groups][0][filters][0][condition_type]=in&searchCriteria[sort_orders][0][field]=created_at&searchCriteria[sort_orders][0][direction]=DESC&searchCriteria[current_page]=1&searchCriteria[page_size]=10
I have a little lib - https://github.com/dsheiko/magentosearchquerybuilder, which helps me building such queries
$builder = new SearchCriteria();
$builder
->filterGroup([
[ "category_id", implode(",", $categories), SearchCriteria::OP_IN ],
])
->sortOrder( "created_at", "DESC")
->limit(1, 10);
echo $builder->toString();

Related

Refine Search on WIQL

WIQL SEARCH:
{
"query": "SELECT [System.Id] FROM WorkItems WHERE [System.Title] Contains Words 'midserver' AND [System.AreaPath] = 'XXXXX' AND [System.WorkItemType]='Issue' AND [System.State]<>'Done' ORDER BY [System.Id]"
},
can you pls help me with a query which refines the search i.e. the query should search the exact words and not CONTAINS ([System.Title] CONTAINS 'Search Text') –
something like IS ([System.Title], i have tried that but it doesn't recognize the query i think "IS" is not recognized
for e.g. story contains following names "rahul 1", and "rahul 2"..but if Iam searching with only "rahul" it should not display "rahul1" and "rahul2" instead it should say something like not found
Observation: its not working if there is space in the user story when i use Contains Words
so basically searching for the exact text if it is there or not but not search with contains
Since you want to search the exact words, why not just use a " = ". Change your WIQL like this:
SELECT
[System.Id]
FROM WorkItems
WHERE
[System.Title] = 'midserver'
AND [System.WorkItemType]='Issue'
AND [System.State]<>'Done'
ORDER BY [System.Id]
Actually the detail supported operators you are using such as =, Contains, Under is based on Field type.
Not all operators could used for each field type. For details, you could take a look at this screenshot:
If you are using System.Title which is a string field
Title
A short description that summarizes what the work item is and helps
team members distinguish it from other work items in a list. Reference
name=System.Title, Data type=String
So instead of Contains Words, you could directly use "=" in your case. For other kind of fields, you need to follow above operators.

Gremlin: Generate a list by location of counts for active versus inactive users

I have vertices people, usertype, and location. People has outgoing edges people_location and people_usertype. People has property 'name', usertype has property 'activationStatus', and location has property 'name'.
I want to create a list that looks like this:
[[1]: https://i.stack.imgur.com/lKzZL.png]
I want the count of people, by location, for activationStatus "active" and "inactive" where the location has "US" in it.
This is all I have only for count of people by location where the location 'name' begins with US:
g.V()hasLabel('people').out('people_publicisofficelocation')
.filter(has('name',between('US','UT')))
.groupCount().by('name')
It is running but not yielding results.
You can simulate 'starts with' behavior in versions of TinkerPop prior to 3.4 using something like has('name',between('US','UT')) so you could replace the filter line above with that. If the graph implementation you are using supports TinkerPop 3.4 there are additional text predicates you can use for begins with, ends with and contains.
As others have said if you can post some sample addV() and addE() steps that build part of your graph it will be easier to give a more precise answer.
This worked for me!
g.V().hasLabel('Location').filter(has('name',between('US','UT')))
.project('Name','Active', 'Inactive', 'Total')  .by('name')  .by(__.both('people_location').out('people_usertype')
.where(values('activationStatus').is(eq('Active'))).count())  .by(__.both('people_location').out('people_usertype')
.where(values('activationStatus').is(eq('Inactive'))).count()) 
.by(__.both('people_location').out('people_usertype').count())

Multiple "LIKE" string search on Algolia

I'm currently researching on functionalities on Algolia for a location service. I have a simple question.
Is it possible to search with multiple "like" string, something similar to as below in MySQL?
select * from route
WHERE pickup LIKE "51%"
AND dropoff LIKE "80%";
On Algolia, let's assume there is a simple index consists of small data:
[{
"pickup" : "51105",
"dopoff" : "80637"
},
{
"pickup" : "51105",
"dopoff" : "39871"
},
{
"pickup" : "32791",
"dopoff" : "40545"
}]
I've checked it can be retrieved by given exactly two params, like:
index.search({
facetFilters: 'pickup: 51105, dropoff:80637',
}
Also we can use normal query with typo for one of target params,
index.search({
query: '51',
restrictSearchableAttributes: [
'pickup'
],
}
but neither of them is satisfied with my original requirement.
I've found a post that might be related to my question in algolia community, but It woundn't give me much insights.
Querying multiple terms in multiple fields
I'd think there is a solution as I believe this'd be a quite common use-case.
It would be great if anybody has encountered similar situation or some insights. Thanks a lot.
I think the best equivalent of MySQL LIKE queries applied to facets would be to use searchForFacetValues.
Here is the documentation about the feature: https://www.algolia.com/doc/api-client/javascript/search/#search-for-facet-values

Yii framework: CMenu - get messages count

I am learning yii framework and i have problem. I have CMenu:
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Messages ('. User::model()->with('messages_count')->find() .')', 'url'=>array('/message/index')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
and relation in User model:
'messages_count' => array(self::STAT,'Message','owner_id'),
In site that shows me 'Messaages (admin)', but I expect 'Messages (3)', so I think this line is wrong:
User::model()->with('messages_count')->find()
But I don't know how to get only count. Can you help me?
P.s Is there possibility to add criteria to relation?
find() finds a single active record with the specified condition.
count() finds the number of rows satisfying the specified query condition.
Your sollution:
User::model()->with('messages_count')->count();
With CDbCriteria:
$criteria=new CDbCriteria();
$criteria->with = 'messages_count';
// your extra criteria
User::model()->count($criteria);
Be sure to check out the Yii documentation especially if you're learning to work with Yii and CActiveRecord: http://www.yiiframework.com/doc/api/1.1/CActiveRecord

How can we use like in yii Mongodb?

Could any one please tell me the syntax for like in yii mongodb .I would like to search for a particular name using like as in sql from mongo db collection.How can we write the query for this.
You can use the MongoRegex object: http://php.net/manual/en/class.mongoregex.php which is compatible with components like YiiMongoDBSuite.
It uses a regular expression to match so %sammaye% in SQL = new MongoRegex('/sammaye/') in MongoDB.
Edit
To search by sammaye% you can do new MongoRegex('/^sammaye/') as asked here: https://stackoverflow.com/questions/13194639/like-function-with-yii-mongo-db
If you use the ActiveRecord in Yii2, you should choose simple like part in the where condition:
ActiveRecord::findOne(['like', 'field', 'query'])
For more information see related docs: http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail
or of course there are also available the PHP's MongoDB commands:
$mongo = \Yii::$app->mongodb;
$collection = $mongo->getCollection('your collection name');
$result = $collection->find(['field' => new \MongoDB\BSON\Regex('^stratswith')]);
Hope it helps!
There is no like support. But based on your requirement, you can use the regex support.
Below are the examples from their SQL to MongoDB query mapping chart.
SELECT * FROM users WHERE name LIKE "%Joe%" = db.users.find({name:/Joe/})
SELECT * FROM users WHERE name LIKE "Joe%" = db.users.find({name:/^Joe/})