Filter array elements with $regex - regex

///sample Data
{
"_id" : "CUST1234",
"Phone Number" : "9585290750",
"First Name" : "jeff",
"Last Name" : "ayan",
"Email ID" : "",
"createddate" : 1462559400000.0,
"services" : [
{
"type" : "Enquiry",
"timeSpent" : "0:00",
"trxID" : "TRXE20160881",
"CustomerQuery" : "Enquiry about travell agent numbers in basaveshwara nagara",
"ServiceProvided" : "provided info through whatsapp",
"Category" : "Tours/Travels",
"callTime" : "2016-05-06T18:30:00.000Z",
"ActualAmount" : 0,
"FinalAmount" : 0,
"DiscountRuppes" : 0,
"DiscountPerctange" : 0
},
{
"type" : "Enquiry",
"timeSpent" : "0:00",
"trxID" : "TRXE20160882",
"CustomerQuery" : "Enquiry about Electric bill payment of house",
"ServiceProvided" : "Service provided",
"Category" : "Utility Services",
"callTime" : "2016-05-10T18:30:00.000Z",
"ActualAmount" : 0,
"FinalAmount" : 0,
"DiscountRuppes" : 0,
"DiscountPerctange" : 0
},
{
"type" : "Enquiry",
"timeSpent" : "0:00",
"trxID" : "TRXE20160883",
"CustomerQuery" : "Enquiry about KPSC office number",
"ServiceProvided" : "provided info through whatsapp",
"Category" : "Govt Offices/Enquiries",
"callTime" : "2016-05-13T18:30:00.000Z",
"ActualAmount" : 0,
"FinalAmount" : 0,
"DiscountRuppes" : 0,
"DiscountPerctange" : 0
},
{
"type" : "Enquiry",
"timeSpent" : "0:00",
"trxID" : "TRXE20160884",
"CustomerQuery" : "Enquiry about Sagara appolo hospital contact number",
"ServiceProvided" : "provided the information through call",
"Category" : "Hospitals/Equipments",
"callTime" : "2016-05-14T18:30:00.000Z",
"ActualAmount" : 0,
"FinalAmount" : 0,
"DiscountRuppes" : 0,
"DiscountPerctange" : 0
},
]
}
Expected Output : entire data that matches particular string in search box from "services" field.
db.collection.aggregate([
{
$match: {
"Phone Number": "9585290750",
"services": { $regex: "/^t/", $options: "s i" }
}
},
{
$project: {
"Services": "services"
}
}
]);
I am facing an issue in regex portion in the above Collection, services is an array field. Please help me to filter the data.

Guys since i am new to Mongodb it took me a day to find a proper solution to my task. I have a solution to my issue. If you guys have better query than this, just post it or modify it....
db.collections.aggregate([
{"$match":{"Corporate_ID":"id"}},
{"$unwind":"$services"},
{"$match":{"$or":[
{"services.type":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.timeSpent":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.trxID":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.CustomerQuery":{$regex:'F',"$options": "i"}},
{"services.ServiceProvided":{$regex:'F',"$options": "i"}},
{"services.Category":{$regex:'F',"$options": "i"}},
{"services.callTime":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.ActualAmount":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.FinalAmount":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.DiscountRuppes":{$regex:'TRXF2016088142',"$options": "i"}},
{"services.DiscountPerctange":{$regex:'TRXF2016088142',"$options": "i"}}
]}},
{"$unwind":"$services"},
{"$project":{
"service":"$services"}
}
])

This is because you are passing in a string of JavaScript regular expression object to $regex. Change your regex to one of the following.
"service": { "$regex": /^t/, "$options": "si" }
or
"service": { "$regex": "^t", "$options": "si" }

Related

update dynamodb list item

This seems like it should be really simple but I haven't found any examples or documentation. I've got a dynamodb table that looks like this:
record 1: {name, email, items[{product}, {item2}, {item3]}
record 2: (name, email, items[{product}, {item2}, {item3]}
I need to be able to update items elements, i.e., update item1 object in record 1. I can do this with the following code by hardcoding the list array element, but I can't figure out how to pass the item number into the update expression :
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : { "S" : "${context.arguments.input.id}" }
},
"update" : {
"expression" : "SET #items[0].#product= :productVal",
"expressionNames" : {
"#product": "product",
},
"expressionValues" : {
":productVal": { "S" : "${context.arguments.input.product}" },
}
}
Have you tried something like:
"update" : {
"expression" : "SET #items[:idx].#product= :productVal",
"expressionNames" : {
"#product": "product",
},
"expressionValues" : {
":productVal": { "S" : "${context.arguments.input.product}" },
":idx": { "N" : 0 }
}
}

Regexp query seems to be ignored in elasticsearch

I have the following query:
{
"query" : {
"bool" : {
"must" : [
{
"query_string" : {
"query" : "dog cat",
"analyzer" : "standard",
"default_operator" : "AND",
"fields" : ["title", "content"]
}
},
{
"range" : {
"dateCreate" : {
"gte" : "2018-07-01T00:00:00+0200",
"lte" : "2018-07-31T23:59:59+0200"
}
}
},
{
"regexp" : {
"articleIds" : {
"value" : ".*?(2561|30|540).*?",
"boost" : 1
}
}
}
]
}
}
}
The fields title, content and articleIds are of type text, dateCreate is of type date. The articleIds field contains some IDs (comma-separated).
Ok, what happens now? I execute the query an get two results: Both documents contain the words "dog" and "cat" in the title or in the content. So far it's correct.
But the second result has the number 3507 in the articleIds field which doesn't match to my query. It seems that the regexp is ignored because title and content already match. What is wrong here?
And here's the document that should not match my query but does:
{
"_index" : "example",
"_type" : "doc",
"_id" : "3007780",
"_score" : 21.223656,
"_source" : {
"dateCreate" : "2018-07-13T16:54:00+0200",
"title" : "",
"content" : "Its raining cats and dogs.",
"articleIds" : "3507"
}
}
And what I'm expecting is that this document should not be in the results because it contains 3507 which is not part of my query...

How to set relation for inner array object in Loopback?

I have data in a table like below
{
"_id" : ObjectId("5a9d1b70b826170df4365489"),
"userId" : "5a93e0b76d32cd0e6c1b99aa",
"mediaId" : "5a99330af218d30c981cda2f",
"comment" : "Hi this is a video",
"**replies**" : [
{
"comment" : "this is a reply 1",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa"
},
{
"comment" : "this is a reply 2",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa"
},
{
"comment" : "this is a reply 3",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa"
}
],
"createdAt" : "",
"updatedAt" : ""
}
My question:
How to set a relation and include data from User table for userId in every object of replies array in loopback?
My expected Output:
{
"_id" : ObjectId("5a9d1b70b826170df4365489"),
"userId" : "5a93e0b76d32cd0e6c1b99aa",
"mediaId" : "5a99330af218d30c981cda2f",
"comment" : "Hi this is a video",
"**replies**" : [
{
"comment" : "this is a reply 1",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa",
"userDetails":{
"name":"xxxxxxxxx",
"address":"xxxxxxxx"
}
},
{
"comment" : "this is a reply 2",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa",
"userDetails":{
"name":"xxxxxxxxx",
"address":"xxxxxxxx"
}
},
{
"comment" : "this is a reply 3",
"**userId**" : "5a93e0b76d32cd0e6c1b99aa",
"userDetails":{
"name":"xxxxxxxxx",
"address":"xxxxxxxx"
}
}
],
"createdAt" : "",
"updatedAt" : ""
}
I am getting stuck up here.Kindly share some solutions
Do the following
create 3 models: yourModel, user, reply
set these relations in model definition json or using node api:
yourModel embedsMany Replies, Reply belongsTo user, user
hasMany Replies
query data using a stringified json as include filter
https://loopback.io/doc/en/lb3/Querying-data.html#using-stringified-json-in-rest-queries
{include: {relatedModel1: [{relatedModel2: 'relationName'} , 'relatedModel']}}
something like this maybe
/api/your_model/?filter={"include":{"replies":{"userDetails":true}}}

Using multiple location fields in ElasticSearch + Django-Haystack

I'm using django-haystack and ElasticSearch to index Stores.
Until now, each store had one lat,long coordinate pair; we had to change this to represent the fact that one store can deliver products to very different regions (disjunct) I've added up to ten locations (lat,long pairs) to them.
When using one location field everything was working fine and I got right results. Now, with multiple location fields, I can't get any results, not even the previuos one, for the same user and store coordinates.
My Index is as following:
class StoreIndex(indexes.SearchIndex,indexes.Indexable):
text = indexes.CharField(document=True, use_template=True,
template_name='search/indexes/store/store_text.txt')
location0 = indexes.LocationField()
location1 = indexes.LocationField()
location2 = indexes.LocationField()
location3 = indexes.LocationField()
location4 = indexes.LocationField()
location5 = indexes.LocationField()
location6 = indexes.LocationField()
location7 = indexes.LocationField()
location8 = indexes.LocationField()
location9 = indexes.LocationField()
def get_model(self):
return Store
def prepare_location0(self, obj):
# If you're just storing the floats...
return "%s,%s" % (obj.latitude, obj.longitude)
# ..... up to prepare_location9
def prepare_location9(self, obj):
# If you're just storing the floats...
return "%s,%s" % (obj.latitude_9, obj.longitude_9)
Is this the correct way to build my index?
From elasticsearch I get this mapping information:
curl -XGET http://localhost:9200/stores/_mapping?pretty=True
{
"stores" : {
"modelresult" : {
"properties" : {
"django_id" : {
"type" : "string"
},
"location0" : {
"type" : "geo_point",
"store" : "yes"
},
"location1" : {
"type" : "geo_point",
"store" : "yes"
},
"location2" : {
"type" : "geo_point",
"store" : "yes"
},
"location3" : {
"type" : "geo_point",
"store" : "yes"
},
"location4" : {
"type" : "geo_point",
"store" : "yes"
},
"location5" : {
"type" : "geo_point",
"store" : "yes"
},
"location6" : {
"type" : "geo_point",
"store" : "yes"
},
"location7" : {
"type" : "geo_point",
"store" : "yes"
},
"location8" : {
"type" : "geo_point",
"store" : "yes"
},
"location9" : {
"type" : "geo_point",
"store" : "yes"
},
"text" : {
"type" : "string",
"analyzer" : "snowball",
"store" : "yes",
"term_vector" : "with_positions_offsets"
}
}
}
}
}
Then, I try to query this way:
sqs0 = SearchQuerySet().dwithin('location0', usuario, max_dist).distance('location0',usuario).using('stores')
where:
usuario is a Point instance representing the user trying to find stores near his position and
max_dist is a D instance.
If I query directly, using curl I got no results, too.
Here is the result of quering using curl with multiple location fields:
$ curl -XGET http://localhost:9200/stores/modelresult/_search?pretty=true -d '{ "query" : { "match_all": {} }, "filter" : {"geo_distance" : { "distance" : "6km", "location0" : { "lat" : -23.5, "lon" : -46.6 } } } } '
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
If comment out the fields location1-9 from the StoreIndex class everything works fine, but if I leave them to get multiple location points, I get no results for the same query (user position). This happens for the same query, in django as directly, using curl. That is, if I have only one location (say location0), both queries returns correct results. With more locations (location0-9), both queries didn't give any results.
Here's the results of quering directly using curl with only one location field:
$ curl -XGET http://localhost:9200/stores/modelresult/_search?pretty=true -d '{ "query" : { "match_all": {} }, "filter" : {"geo_distance" : { "distance" : "6km", "location0" : { "lat" : -23.5, "lon" : -46.6 } } } } '
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9,
"max_score" : 1.0,
"hits" : [ {
"_index" : "stores",
"_type" : "modelresult",
"_id" : "store.store.110",
"_score" : 1.0, "_source" : {"django_ct": "store.store", "text": "RESULT OF THE SEARCH \n\n", "django_id": "110", "id": "store.store.110", "location0": "-23.4487554,-46.58912"}
},
lot's of results here
]
}
}
Of course, I rebuild_index after any change in StoreIndex.
Any help on how to get multiple location fields working with elasticsearch and django?
PS.: I've cross posted this question on Django-Haystack and ElasticSearch Google Groups.
https://groups.google.com/d/topic/elasticsearch/85fg7vdCBBU/discussion
https://groups.google.com/d/topic/django-haystack/m2A3_SF8-ls/discussion
Thanks in advance
Mário

Mongodb regex embedded search

I got these documents:
id
name
friendList
id
name
I need to search in php (mongodb) for a friend, so name in friendList. How can I do this?
In the shell you'd do it like this:
> db.people.find({ "friendList.name" : /Joe/})
UPDATE: a proof:
> db.person.insert({name : 'scatman', friendList : [ {name: 'joe'}, {name: 'nick'} ]});
> db.person.findOne()
{
"_id" : ObjectId("4f155cafef7b8b0317a8ad17"),
"name" : "scatman",
"friendList" : [
{
"name" : "joe"
},
{
"name" : "nick"
}
]
}
> db.person.findOne({"friendList.name" : /jo/})
{
"_id" : ObjectId("4f155cafef7b8b0317a8ad17"),
"name" : "scatman",
"friendList" : [
{
"name" : "joe"
},
{
"name" : "nick"
}
]
}
>