Wondering if this from the Elasticsearch official doc:
{
"more_like_this" : {
"fields" : ["name.first", "tweet"],
"like" : [
{
"_index" : "marvel",
"_type" : "quotes",
"doc" : {
"name": {
"first": "Ben",
"last": "Grimm"
},
"tweet": "You got no idea what I'd... what I'd give to be invisible."
}
},
],
"min_term_freq" : 1,
"max_query_terms" : 1
}
}
is yet implemented within the latest release of Elastica?
The bit I am struggling with is the "doc" section of the "like".
My code is as follow:
$moreLikeThis = (new Query\MoreLikeThis())
->setFields([
'name.first',
'tweet'
])
->setLike((new Document())
->setIndex('myIndexName')
->setType('myTypeName')
->setData([
'tweet' => 'Foo',
'name' => [
'first' => 'Bar',
'last' => 'Test'
]
])
)
->setMinTermFrequency(1)
->setMinDocFrequency(1);
But it looks like the query is not generated properly. Here is what I get when I var_dump() Request::toString():
string(398)
"{"path":"myIndexName/myTypeName/_search","method":"GET","data":{"query":{"more_like_this":{"fields":["name.first","tweet"],"like":{"_id":"","_type":"myTypeName","_index":"myIndexName"},"min_term_freq":1,"min_doc_freq":1}}},"query":{"search_type":"count"},"connection":{"config":[],"host":"localhost","port":9200,"enabled":true}}"
The "doc" section if definitely missing? Am I not using it properly?
If anyone is wondering, the feature was actually not fully implemented within Elastica.
It's fixed now.
Related
I'm using this package https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operations to deal with a mongo on the project I'm working on.
I have collection with such a structure (here is a example of one document)
{
"_id" : ObjectId("5fda3a602279e5262a3ddec6"),
"type" : "type",
"tags" : [
"tag1",
"tag2",
],
"filters" : [
{
"key" : "1",
"label" : "some key1"
},
{
"key" : "2",
"label" : "some key2"
}
],
"updated_at" : ISODate("2020-12-16T16:48:32.000+0000"),
"created_at" : ISODate("2020-12-16T16:48:32.000+0000")
},
what I need to do is check if collection has a document with a same type and exact same tags as provided, if yes I need to update that document if not create it.
Here is a code I wrote:
$this->collection->where('type', $type)
->where('tags', 'all', $tags)
->update(
['type' => $type, 'tags' => $tags, 'filters' => $filters,],
['upsert' => true,]
);
here I'm checking if we have a document with a type equal to $type and tags same as $tags, if such document exists I update it (replace all data with a new one) if not it should create a new document, thanks to ['upsert' => true,]
But when I'm running it I'm getting error cannot infer query fields to set, path 'tags' is matched twice
FYI: when I'm removing ->where('tags', 'all', $tags) query is working, not sure if correct, but it's not failing
Also I tried to do it like this: ->where('tags', ['$all' => $tags]) and issue is the same as above
Updated it's happening only when it trying to insert new field, updates work correctly
I have a MongoDB document structure like this:
[
{
"locale":"en",
"translations":[
{
"name":"translation1",
"value":"enValue"
},
{
"name":"translation2",
"value":"enValue"
},
{
"name":"translation3",
"value":"enValue"
}
]
},
{
"locale":"ru",
"translations":[
{
"name":"translation1",
"value":"ruValue"
},
{
"name":"translation2",
"value":"ruValue"
},
{
"name":"translation3",
"value":"ruValue"
}
]
}
]
and I need to get the translation with name translation1 for locale en.
The expected result I want is:
{
"_id" : ObjectId("5e845ba1005e625a6237d2e0"),
"translations" : [
{
"name" : "translation1",
"value" : "enValue"
}
]
}
I know how to do this with pure mongo, it should be like this:
db.translations.find({"locale" : "en"},
{ translations: { $elemMatch: { name: "translation1" } } } )
Here is the proof https://gyazo.com/fb9b1a505a898c7137ece5304d715171
but I can't make it work with PHP. I tried code like:
$collection = $this->database->{$group};
$collection->find(
[
'locale' => 'en',
'translations' => ['$elemMatch' => ['name' => 'translation1']
]
);
And Im getting all translations for en instead of only tranlsation1 as a result:
{
"_id" : ObjectId("5e845ba1005e625a6237d2e0"),
"locale" : "en",
"translations" : [
{
"name" : "translation1",
"value" : "enValue"
},
{
"name":"translation2",
"value":"enValue"
},
{
"name":"translation3",
"value":"enValue"
}
]
}
I tried as:
$collection = $this->database->{$group};
$collection->find(
['locale' => 'en'],
[
'translations' => ['$elemMatch' => ['name' => 'translation1']
]
);
also result is the same as above.
Tried like:
$collection = $this->database->{$group};
$collection->find(
[
'locale' => 'en',
[
'translations' => ['$elemMatch' => ['name' => 'translation1']
]
]
);
result is null
As a workaround, for now, I filter result on PHP side, but it extra work
This appears to be a bug in the driver.
This database command should be equivalent to the find you were running, but the command works as expected while the find does not.
$this->$database->command([
'find'=>'CollectionName',
'filter'=>['locale' => 'en'],
'projection'=>['translations' => ['$elemMatch' => ['name' => 'translation1']]]
])
I'm working on a project and try to make a search with elasticsearch but my field can contain dash and when I search with it I can't find the result I'm looking for, so I tried to change the mapping but the index doesn't work at all. I don't have any error message but I can't find what I indexed even using a different field. So what I did was :
$params = [
'index' => 'arc',
'type' => 'purchase',
'id' => $purchase['id'],
'body' => $purchase
];
It worked great with that except for the field with the dash. My $purchase looks like that :
array:34 [
"id" => 163160
"distant" => "MOR-938BBM28147090"
[...]
]
so when I search for "MOR" I find the result but when I do "MOR-" nothing. I tried to change the mapping by doing that :
$params = [
'index' => 'arc',
'type' => 'purchase',
'id' => $purchase['id'],
'body' => [
'mappings' => [
'_default_' => [
'properties' => [
'distant' => [
'type' => 'string',
'index' => 'not_analyzed'
]
]
]
],
$purchase
]
];
But with that even if I try to search "163160" I can't find any result.
Whitespace analyzer could be the right solution in this case. It takes into account only whitespaces while breaking text into tokens, and characters like "-" or "_" are still treated as a part of a term.
But if you need to do a partial matching, for example with "MOR-" token, then it requires a bit more complicated mapping.
As I don't know php, I'll be using Elasticsearch syntax. First, create a proper mapping:
PUT http://127.0.0.1:9200/arc
{
"settings": {
"analysis": {
"analyzer": {
"edge_ngram_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 18,
"token_chars": [
"letter",
"digit",
"punctuation"
]
}
}
}
},
"mappings": {
"purchase": {
"properties": {
"distant": {
"type": "string",
"analyzer": "edge_ngram_analyzer"
}
}
}
}
}
As you can see, I use EdgeNGram tokenizer here. When you index a document with MOR-938BBM28147090 in distant field, it will create following tokens:
[MOR, MOR-, MOR-9, MOR-93, MOR-938, MOR-938B, MOR-938BB, ...]
The core point here is punctuation character class in token_chars list, that tells elasticsearch, that dash character (and some others like ! or ") should be included in a token and not treated as a "split char".
Now when I index the document:
PUT http://127.0.0.1:9200/arc/purchase/163160
{
"distant": "MOR-938BBM28147090"
}
and run a term search query:
POST http://127.0.0.1:9200/arc/purchase/_search
{
"query": {
"bool" : {
"must" : {
"term" : {
"distant": "MOR-93"
}
}
}
}
}
I get in response:
"hits": {
"total": 1,
"max_score": 0.6337049,
"hits": [
{
"_index": "arc",
"_type": "purchase",
"_id": "163160",
"_score": 0.6337049,
"_source": {
"distant": "MOR-938BBM28147090"
}
}
]
}
I am using jenssegers/laravel-mongodb library in a laravel application however I need to show counts of an embedded document. Using a generic example of comment/posts, while I can solve my problem by just pulling all the posts and looping through to get comments to count them but was just was not sure if I could query them.
I did set up my relationships. In my post class I did:
public function comments()
{
return $this->hasMany('App\Comment');
}
and in my comment class:
public function post()
{
return $this->belongsTo('App\Post');
}
Later in code:
$post->comments()->save($comment);
$comment->post()->associate($post);
my document structure:
"posts" : [
{
"_id" : ObjectId("5805a11e2594ee26543ea041"),
"Post_Num" : "166236001010",
"updated_at" : ISODate("2016-10-18T04:12:14.454Z"),
"created_at" : ISODate("2016-10-18T04:12:14.451Z"),
"comments" : [
{
"Comment_Num" : "3333333",
"_id" : ObjectId("5805a11e2594ee26543ea042"),
"post_id" : "5805a11e2594ee26543ea041",
},
{
"Comment_Num" : "3333333",
"_id" : ObjectId("5805a11e2594ee26543ea042"),
"post_id" : "5805a11e2594ee26543ea041",
}
]
},
{
"_id" : ObjectId("5805a11e2594ee26543ea041"),
"Post_Num" : "166236001010",
"comments" : [
{
"Comment_Num" : "3333333",
"_id" : ObjectId("5805a11e2594ee26543ea042"),
"post_id" : "5805a11e2594ee26543ea041",
}
]
}
]
Now when I try getting the comments like:
$post->comments()->count()
or
$post->comments()->get()->count()
or
$post->comments->get()->count()
I get a 0. The same logic works if it is not an embedded document but just was wondering if it was possible to do an aggregate query ? Perhaps is best to just let the code iterate and add everything?
As you can tell I need some minor hand holding. Thank You
UPDATE: I am trying the following
public function commentCount()
{
$commentsCount = Post::raw(function($collection)
{
return $collection->aggregate(['$project' => ['_id' => 1,
'comments_count' => ['$size' => '$comments']],
['$group' => ['_id' => null, 'count' => ['$sum' => '$comments_count']]]]);
});
return $commentsCount;
}
What I get now is:
$pipeline is not a list (unexpected index: "$project")
Just to be clear, you want a list of your posts with the number of comments on each post?
Aggregation has something to offer for that:
https://docs.mongodb.com/manual/reference/operator/aggregation/size/#exp._S_size
I'm not a php dev but this is my shot at it:
Post::raw()->aggregate(
['$project' => ['_id' => 1,
'Post_Num' => 1,
'comments_count' => ['$size' => '$comments']],
['$group' => ['_id' => null, 'count' => ['$sum' => '$comments_count']]]
]);
I have created query in mongoDB. In MongoChef this query produces more than 10 thousand records in less than 2 seconds. Now I want to execute this query in PHP.
So i don't know how to write query in php as I read various documents on internet but confused how to implement it.
db.PMS.aggregate(
[
{$project:
{EventTS:1,MainsPower:1,PanelID:1}
},
{$unwind:
{path:"$MainsPower",includeArrayIndex:"arrayIndex",preserveNullAndEmptyArrays:true}
},
{ $match: { "MainsPower":{$ne:null}}},
{ $match: { "EventTS":{$gt:new Date("2016-01-01")}}},
{$project:
{MainsPower:1,
PanelID:1,
timestamp:{"$add":
[{'$subtract' : ["$EventTS",new Date("1970-01-01")]},
{"$multiply":[60000,"$arrayIndex"]}
]}
}
}
]
);
You can use some resources available on the php official documentation. A mapping of sql queries in php to mongoDB queries in php can be found here.
Also I have a demo login and registration script at my github. You can view those in this repo.
If you use MongoDB PHP Library you should be able to do something similar to this:
$mongo = new MongoClient();
$database = $mongo->examples;
$collection = $database->PMS;
$pipeline = [
[
'$project' => [
'EventTS' => 1,
'MainsPower' => 1,
'PanelID' => 1,
]
],
[
'$unwind' => [
'path' => '$MainsPower',
'includeArrayIndex' => 'arrayIndex',
'preserveNullAndEmptyArrays' => true
]
],
...
];
$cursor = $collection->aggregate($pipeline);