Elasticsearch php giving error when setting 2 criterias in must clause - php

I am trying to add multiple criterion to ElasticSearch PHP in must clause, but it is giving me a 500 Error.
My params are:
$params = ['index' => 'smartjn',
'type' => 'feed_details',
'size' => 10,
'body' => [
'sort' => [
'sorter' => [
'order' => 'desc',
'mode' => 'max'
]
],
'query' => ['bool' => [
'must' => [
['match' => ['approvalStatus' => 'approved']],
[ '_id' => [
'$lt' => new MongoDB\BSON\ObjectId($documentid)
]
]
],
'must_not' => ['term' => ['muteFeedUserIds' => $userID]],
'must_not' => ['term' => ['muteUserIds' => $userID]]
]
]
]
];
Please note that this works if I remove the second criteria in must clause i.e.
$params = ['index' => 'smartjn',
'type' => 'feed_details',
'size' => 10,
'body' => [
'sort' => [
'sorter' => [
'order' => 'desc',
'mode' => 'max'
]
],
'query' => ['bool' => [
'must' => [
['match' => ['approvalStatus' => 'approved']]
],
'must_not' => ['term' => ['muteFeedUserIds' => $userID]],
'must_not' => ['term' => ['muteUserIds' => $userID]]
]
]
]
];
Any idea what am I missing?
Thanks

check this out.
'query' => ['bool' => [
'must' => [
['match' => ['approvalStatus' => 'approved']],
[ 'ids' => [
'values' => [new MongoDB\BSON\ObjectId($documentid)]
]
]
],
'must_not' => ['term' => ['muteFeedUserIds' => $userID]],
'must_not' => ['term' => ['muteUserIds' => $userID]]
]
If that doesnt work i am giving you the raw as i am not good with elasticphp
{
"query": {
"bool": {
"must": [
{
"ids": {
"type": "type_of_index"
"values": ["AVbqtjXfoKQ0pubF_-NA"]
}
}
]
}
}
}
type is optional
Just add this ids part under must area.

Related

Why can't I use my analyzer and get answer 'failed to find analyze'?

I made my index with analyzer like in documentation (there).
This is my index create:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'index_analyzer' => [
'my_index_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'mynGram2'
],
],
],
'search_analyzer' => [
'my_search_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'standard',
'lowercase',
'mynGram2'
],
],
],
'filter' => [
'mynGram2' => [
'type' => 'nGram',
'min_gram' => 2,
'max_gram' => 20,
],
],
],
'max_ngram_diff' => 50,
],
],
];
$x = $this->obj->indices()->create($params);
Then i try use my analyzer:
$params = [
'index' => 'mytestindex',
'body' => [
'analyzer' => 'my_search_analyzer',
'text' => 'текст проверить чтобы'
],
];
$x = $this->obj->indices()->analyze($params);
But I get this message:
'{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[PEREGOVOR2][127.0.0.1:9300][indices:admin/analyze[s]]"}],"type":"illegal_argument_exception","reason":"failed
to find analyzer [my_search_analyzer]"},"status":400}'
So... what am I doing wrong? Why can't I use my analyzer and get answer 'failed to find analyze'?
You're not building your analyzer correctly. You only need one analyzer section in your settings:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [ <--- change this
'my_index_analyzer' => [
'type' => 'custom',
"tokenizer" => "standard",
'filter' => [
"lowercase",
"mynGram2"
],
],
'my_search_analyzer' => [
"type" => "custom",
"tokenizer" => "standard",
'filter' => [
"standard",
"lowercase",
"mynGram2"
],
],
],
'filter' => [
'mynGram2' => [
"type" => "nGram",
"min_gram" => 2,
"max_gram" => 20,
],
],
],
"max_ngram_diff" => "50",
],
],
];
$x = $this->obj->indices()->create($params);

Elasticsearch how to correctly provide a negative boost in PHP

I'm trying to give a negative boost to push results down in the ranking if they have 'b-stock' in the title.
Here is my code:
'body' => [
'size' => 15,
'query' => [
'boosting' => [
'positive' =>[
'bool' => [
'should' => [
['query_string' => [
'default_field' => 'title_tag',
'query' => $term
]],
['query_string' => [
'default_field' => 'name',
'query' => $term
]],
['query_string' => [
'default_field' => 'description',
'query' => $term
]],
]
],
],
'negative' => [
'term' => [
'name' => 'B-Stock'
]
],
'negative_boost' => 2
]
]
]
However this seems to have no affect on the results even if I remove the term array from the 'negative' array the same results set is returned.

Elasticsearch search results filtering

I am new to Elasticsearch and I am using REST API for PHP to play around with data returned. I am using following code to retrieve data.
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
Above code returns data perfectly matching "validated=>true" and "image=>true".
Further I want to add open text search like we use /_search/?q=Apple macbook. I have tried to use match, multi_match, query_string options, but couldn't get success.
So, I want to retrieve results from ES that have "validated=>true", "image=>true" and matches with text "Apple macbook".
Thanks in advance.
You can try with query_string or simple_query_string
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'simple_query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
you can also do this by
enabling all_field mapping for your index, you can do that by following the below URL
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html
and then use the below ES query:
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ '_all' => 'Apple macbook' ] ],
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];

How do i highlight my result in elasticsearch?

I am not able to highlight my result, which part of my query is wrong?
PHPClient for elasticsearch throws exception on execution.
$query = [
"query" => [
"filtered" => [
"query" => [
"bool" => [
"should" => [
[
'query_string' => [
'fields' => [
'Title.title^4',
'Title.ngrams_front^2',
'Title.ngrams_back'
],
'defaultOperator' => 'or',
'query' => $paramsObj->q
]
],
[
'query_string' => [
'auto_generate_phrase_queries' => 0,
'enable_position_increments' => false,
'fields' => [
'Title.title',
'Address',
'keys'
],
'query' => $paramsObj->q,
'use_dis_max' => false,
'boost' => 2
]
],
[
'fuzzy' => [
'Title.title' => [
'value' => $paramsObj->q,
'boost' => 1,
'min_similarity' => 0.5,
'max_expansions' => 20,
'prefix_length' => 0
]
]
]
]
]
],
"filter" => $filters
]
],
"highlight" => [
"fields" => [
'Title.title' => [ "pre_tags" => "<em>", "post_tags" => "</em>" ]
]
]
];
First i tried highlighting at filtered level, then i googled and found out i need to do at query level at top of filtered level, so i did but still it throws exception.
Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException'
If at all anyone can help, kindly help.
Try something like this:
$query = array(
'query' => array(
'bool' => array(
'should' => array(
'fuzzy' => array(
'name' => array(
'value' => $serachstring,
'boost' => 1,
'min_similarity' => 0.5,
'max_expansions' => 20,
'prefix_length' => 0
),
),
// ...
)
),
),
'highlight' => array(
"pre_tags" => "<em>",
"post_tags" => "</em>",
'fields' => array(
'name' => (object) array()
)
),
);

Invalid Input Resource Mongodb Command PHP

I am trying to get around the 30 second cursor time limit and avoid a MongoCursorTimeoutException by using DB->command method as seen here: MongoCursorTimeoutException for aggregate function
When I try to aggregate, command instantly returns:
array(3) { ["ok"]=> float(0) ["errmsg"]=> string(42) "Invalid input resource, " ["code"]=> int(17138) } There is pretty much no documentation on this and the only think I could find is the source code where the error takes place https://github.com/mongodb/mongo/blob/master/src/mongo/db/pipeline/pipeline.cpp#L327
Can someone please help? The pipeline works fine if I run collection->aggregate(pipeline, options) so I don't think it is the pipeline. My code is below:
$connection = new MongoClient( 'mongodb://user:pass#mydb.biz' );
$summaryDB = $connection->selectDB('Summary');
$summaryCollection = $summaryDB->selectCollection('hitSummary');
//agg pipeline
$pipeline =
[
[
'$match' => [
'date' => [
'$gte' => $weekAgo,
'$lt' => $today,
]
]
], [
'$group' => [
'_id' => [
'client' => '$client',
'date' => '$date',
],
//conversions
'z1is' => [ '$sum' => '$actions.sales.import.z1.count' ],
'z2is' => [ '$sum' => '$actions.sales.import.z2.count' ],
'z3is' => [ '$sum' => '$actions.sales.import.z3.count' ],
'z1ps' => [ '$sum' => '$actions.sales.pixel.z1.count' ],
'z2ps' => [ '$sum' => '$actions.sales.pixel.z2.count' ],
'z3ps' => [ '$sum' => '$actions.sales.pixel.z3.count' ],
'z1as' => [ '$sum' => '$actions.sales.apiOnly.z1.count' ],
'z2as' => [ '$sum' => '$actions.sales.apiOnly.z2.count' ],
'z3as' => [ '$sum' => '$actions.sales.apiOnly.z3.count' ],
'z1ss' => [ '$sum' => '$actions.sales.s2s.z1.count' ],
'z2ss' => [ '$sum' => '$actions.sales.s2s.z2.count' ],
'z3ss' => [ '$sum' => '$actions.sales.s2s.z3.count' ],
//clicks
'z1ic' => [ '$sum' => '$actions.click.import.z1.count' ],
'z2ic' => [ '$sum' => '$actions.click.import.z2.count' ],
'z3ic' => [ '$sum' => '$actions.click.import.z3.count' ],
'z1pc' => [ '$sum' => '$actions.click.pixel.z1.count' ],
'z2pc' => [ '$sum' => '$actions.click.pixel.z2.count' ],
'z3pc' => [ '$sum' => '$actions.click.pixel.z3.count' ],
'z1ac' => [ '$sum' => '$actions.click.apiOnly.z1.count' ],
'z2ac' => [ '$sum' => '$actions.click.apiOnly.z2.count' ],
'z3ac' => [ '$sum' => '$actions.click.apiOnly.z3.count' ],
'z1sc' => [ '$sum' => '$actions.click.s2s.z1.count' ],
'z2sc' => [ '$sum' => '$actions.click.s2s.z2.count' ],
'z3sc' => [ '$sum' => '$actions.click.s2s.z3.count' ],
//impressions
'z1ii' => [ '$sum' => '$actions.display.import.z1.count' ],
'z2ii' => [ '$sum' => '$actions.display.import.z2.count' ],
'z3ii' => [ '$sum' => '$actions.display.import.z3.count' ],
'z1pi' => [ '$sum' => '$actions.display.pixel.z1.count' ],
'z2pi' => [ '$sum' => '$actions.display.pixel.z2.count' ],
'z3pi' => [ '$sum' => '$actions.display.pixel.z3.count' ],
'z1ai' => [ '$sum' => '$actions.display.apiOnly.z1.count' ],
'z2ai' => [ '$sum' => '$actions.display.apiOnly.z2.count' ],
'z3ai' => [ '$sum' => '$actions.display.apiOnly.z3.count' ],
'z1si' => [ '$sum' => '$actions.display.s2s.z1.count' ],
'z2si' => [ '$sum' => '$actions.display.s2s.z2.count' ],
'z3si' => [ '$sum' => '$actions.display.s2s.z3.count' ],
]
], [
'$project' => [
'impressions' => [ '$add' => ['$z1ii', '$z2ii', '$z3ii',
'$z1pi', '$z2pi', '$z3pi',
'$z1ai', '$z2ai', '$z3ai',
'$z1si', '$z2si', '$z3si'] ],
'clicks' => [ '$add' => ['$z1ic', '$z2ic', '$z3ic',
'$z1pc', '$z2pc', '$z3pc',
'$z1ac', '$z2ac', '$z3ac',
'$z1sc', '$z2sc', '$z3sc'] ],
'importSales' => [ '$add' => ['$z1is', '$z2is', '$z3is'] ],
'pixelSales' => [ '$add' => ['$z1ps', '$z2ps', '$z3ps'] ],
'apiSales' => [ '$add' => ['$z1as', '$z2as', '$z3as'] ],
's2sSales' => [ '$add' => ['$z1ss', '$z2ss', '$z3ss'] ],
]
]
];
//do something with this
$options = [ 'timeout' => -1 ];
$result = $summaryDB->command(
[
'aggregate' => $summaryCollection,
'pipeline' => $pipeline,
],
$options
);
var_dump($result);
When you run DB command, the first argument is the name of the command as the key and the value is the name of the collection.
In your case you should be passing "hitSummary" as the value of 'aggregate' and not the collection object.
See simple command example here: http://php.net/manual/en/mongodb.command.php

Categories