TextCriteria with diacriticSensitive. Spring Data MongoDB 1.10.9 - spring-data-mongodb

I'm trying to create a search in my MongoDB database to search the "name" field without taking into account the accents
I need to create an index in the field:
// create index
#Indexed
#Field("nombre")
private String nombre;
Check in the BBDD that it is created correctly:
db.empleado_bk.getIndexes();
{
"v" : 2,
"key" : {
"nombre" : 1
},
"name" : "nombre",
"ns" : "elser2.empleado_bk"
}
I modify my repository to search in text without taking accents into account
if (StringUtils.isNoneBlank(dtoFilter.getNombre())) {
query.addCriteria(TextCriteria.forDefaultLanguage().diacriticSensitive(true).matching("nombre"));
}
But when looking for that field, I get the following error:
org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 27 and error message 'text index required for $text query'
Can someone tell me if I'm doing something wrong if I need to do something else

You applied a standard index with #Indexed. To apply a text search index, you need to use #TextIndexed.

Related

MongoDB/PyMongo find_one_and_update and/or find() method doesn't find specified entry, just appends to the end of the collection

I'm attempting to search in an array in my collection, to find by a key named "id". Both the find() method and find_one_and_update() don't return the entry I'm trying to find. The collection is structured like so:
"_id" : {stuff},
"events":[{
"id":"12345",
"date":"01/01"
}, {
"id":"12346",
"date":"02/02"
}]
Trying find() as:
result = db.get_collection("events").find({"events.id" : "12345"})
for item in result:
print(item)
Prints out the entire collection.
Trying to update a specific entry by its id like so will append it to the end.
db.get_collection("events").find_one_and_update({"events.id" : "12345"},{"$set" : {"date" : "somedate"}})
Looks like this afterwards:
"id":"12346",
"date":"02/02"
}], "date" : "somedate"
So, what am I doing wrong with updating and finding here? Every other person seems to have no trouble with this part.
Figured this out on my own, needed to specify which object + field to update in the collection:
db.get_collection("events").find_one_and_update({"events.id" : "12345"},{"$set" : {"events.$.date" : "somedate"}})

MongoDB - How can I check if some element in an array meets a condition with another element of an array?

I have the following objects in my Java APP:
[
{
"from" : "11/12/2020",
"to" : "12/12/2019"
},
{
"from" : "13/12/2020",
"to" : "14/12/2020
}
]
and in my DB I have the following objects:
[
{
"from" : "01/12/2020",
"to" : "14/12/2019"
},
{
"from" : "20/12/2020",
"to" : "30/12/2020
}
]
What query can I write to check if any of the dates from or to from any object in my first Java APP will be gte or lte than any of the from or to values from my DB objects?
Thanks!
You will have to iterate over all DB entries and compare them to your object.
If you are using JPA Repositories to communicate with your DB, you can do that like this:
List<Entry> matches = dataRepository.findAll().stream().filter(dbEntry -> dbEntry.equals(myEntry)).collect(Collectors.asList());
The ArrayList will now contain all of your matches from the Database.

How to split the custom logs and add custom field name in each values on logstash

I want to split the custom logs
"2016-05-11 02:38:00.617,userTestId,Key-string-test113321,UID-123,10079,0,30096,128,3"
that log means
Timestamp, String userId, String setlkey, String uniqueId, long providerId, String itemCode1, String itemCode2, String itemCode3, String serviceType
I try to made a filter using ruby
filter {
ruby{
code => "
fieldArray = event['message'].split(',')
for field in fieldArray
result = field
event[field[0]] = result
end
"
}
}
but I don't have idea how to split the logs with adding field names each custom values as belows.
Timestamp : 2016-05-11 02:38:00.617
userId : userTestId
setlkey : Key-string-test113321
uniqueId : UID-123
providerId : 10079
itemCode1 : 0
itemCode2 : 30096
itemCode3 : 128
serviceType : 3
How can I do?
Thanks regards.
You can use the grok filter instead. The grok filter parse the line with a regex and you can associate each group to a field.
It is possible to parse your log with this pattern :
grok {
match => {
"message" => [
"%{TIMESTAMP_ISO8601:timestamp},%{USERNAME:userId},%{USERNAME:setlkey},%{USERNAME:uniqueId},%{NUMBER:providerId},%{NUMBER:itemCode1},%{NUMBER:itemCode2},%{NUMBER:itemCode3},%{NUMBER:serviceType}"
]
}
}
This will create the fields you wish to have.
Reference: grok patterns on github
To test : Grok constructor
Another solution :
You can use the csv filter, which is even more closer to your needs (but I went with grok filter first since I have more experience with it): Csv filter documentation
The CSV filter takes an event field containing CSV data, parses it, and stores it as individual fields (can optionally specify the names). This filter can also parse data with any separator, not just commas.
I have never used it, but it should look like this :
csv {
columns => [ "Timestamp", "userId", "setlkey", "uniqueId", "providerId", "itemCode1", "itemCode2 "itemCode3", "serviceType" ]
}
By default, the filter is on the message field, with the "," separator, so there no need to configure them.
I think that the csv filter solution is better.

Mongo query item from subarray, having trouble

I'm relatively new to mongodb, and I came into this company's set up that had the database already set up and running. It looks to me like the structure of this "array" isn't actually a proper array. They are saving info that I need in "offer_info_array" - which then has a nested array with an "offer id" which changes a lot between records, and then nested inside that is the info that I need to select. Here is an example of a record.
{
"_id" : ObjectId("52041af3bbf8057203000004"),
"offer_info_array" : {
"128" : {
"affid" : "68",
"s1" : "YJF"
}
},
"city" : "Cleveland",
"state" : "OH",
"zip" : "44111"
}
So from a whole db of records like this one, I need to find all records that have the "affid" of "68" - I realize this database is not structured correctly, but there's not much I can do about that for records that already exist. The "128" is the offer id that varies from record to record.
If anyone has any insight and can help me out with this, I would greatly appreciate it. Thank you!
You can use $where operator which accepts JavaScript function:
db.items.find({$where: function() {
for(var key in obj.offer_info_array)
if(obj.offer_info_array[key].affid == 68)
return true;
return false; }})
This function looks for properties of offer_info_array object and gets value of property by key. Then we verify if property value has affid property equal to 68. If yes, we return true which means objects matches our query. If there is no properties with affid equal to 68, we return false.
Keep in mind, that $where operator do not use indexes.

MongoDB Index Array only partly working

I know there is already a lot on this subject out there, but none of the questions do help me going on.
I have a File-Upload-System via GridFS which generates documents like this:
{ "_id" : ObjectId( "4f3723607b5c0b8d01060000" ),
"filename" : "7326_150970547928_746052928_2521002_2164327_n.jpg",
"filetype" : "image/jpeg",
"title" : "Indexed File 1",
"author" : "chewbacca",
"document" : "Rechnungen",
"keywords" : { "0" : "Darth",
"1" : "Vader",
"2" : "Haut",
"5" : "Putz",
"6" : "Pushy" },
"uploadDate" : Date( 1329013600519 ),
"length" : 61423,
"chunkSize" : 262144,
"md5" : "c73cce0bb6f349007635751f9a1a7ddd" }
As you can see I have a field 'keywords' which is an array of keywords.
I want to build a search-option to search this field comparable to a fulltext search.
Therefor I indexed the field 'keywords' seperately.
db.fs.files.ensureIndex({ keywords : 1 })
Now the problem is, that this works sometimes. Or to say yesterday it worked on some files, but on some it won't find anything.
Assuming I did the Indexing like above, I would think that
> db.fs.files.find({keywords : "Vader"})
would give me the document printed above. Or am I missing something??
(My only explanation why this could be, is: it takes a lot of time to create indexes and it ain't ready yet, which is practically impossible right, or that there is some problem with the options 'background', 'dropDups', 'unique' etc...
I tried everything. I dropped the Indexes with;
> db.fs.files.dropIndexes()
And created them again. Always controlling via
> db.fs.files.getIndexes()
But no, I can't get any results...
I also tried to make the indexing via PHP just after I saved the file in the database.
For that I use the following code:
$gridFS->ensureIndex(array('keywords' => 1), array("unique" => true));
Or also without the unique option.
As I said sometimes this works and I can query the keywords, but sometimes I can't. Or some keywords are found, but those of other documents not.
Could it be that indexes ain't made for every document equally???
Please somebody help me on that, I really don't get the issue here!
Thanks already.
I suggest you to use a true array in the keywords:
"keywords" : ["Darth", "Vader", "Haut", "Putz", "Pushy"],
So, the following is expected to work:
db.fs.files.ensureIndex({ keywords : 1 })
db.fs.files.find({keywords : "Vader"})

Resources