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"
}
]
}
>
Related
I'm trying to filter a query with term and range along with query-string. filter(range) and query string works but not filter(term). am i doing something wrong?
es = Elasticsearch([{'host': '192.168.121.121', 'port': 9200}])
index = Index("filebeat-*",using=es)
search = index.search()
searchStr = "OutOfMemoryError"
search = search.query("query_string", query=searchStr)
search = search.filter('range' , **{'#timestamp': {'gte': 1589399137000 , 'lt': 1589399377000, 'format' : 'epoch_millis'}})
search = search.filter('term' , **{'can.deployment': 'can-*' })
response = search.execute(ignore_cache=True)
print(response.hits.total)
print(response.hits.hits._source.can.deployment)
json:
filter-term - ['hits']['hits']['_source']['can']['deployment']
filter-range- ['hits']['hits']['_source']['#timestamp']
{
"hits" : {
"total" : 138351328,
"max_score" : 6.5700893,
"hits" : [
{
"_index" : "filebeat-6.1.2-2020.05.13",
"_type" : "doc",
"_score" : 2.0166037,
"_source" : {
"#timestamp" : "2020-05-13T01:14:03.354Z",
"source" : "/var/log/gw_rest/gw_rest.log",
"message" : "[2020-05-13 01:14:03.354] WARN can_gw_rest [EventLoopGroup-3-2]: An exceptionCaught() event was fired.OutOfMemoryError,
"fileset" : {...},
"can" : {
"level" : "WARN",
>>>>>>>> "message" : "An exceptionCaught() event was fired- OutOfMemoryError,
"timestamp" : "2020-05-13 01:14:03.354",
>>>>>>>> "deployment" : "can-6b721b93965b-w3we4-4074-9903"
}
}
}
]
}
}
I actually didn't need a filter(term). this worked:
dIds=response['hits']['hits'][1]['_source']['can']['deployment']
print(dIds)
#loop through the response
for i in response['hits']['hits']:
id = i['_source']['can']['deployment']
print(id)
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 }
}
}
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...
///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" }
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