Algolia search where string attribute matches a given string - algolia

I have the following index structure in algolia
I need to perform a query search where the email attribute matches a given email address i.e return all records where email attribute will match that email. I have read about filtering and faceting and I've also tried to apply it in android but it does not do the filtering that I need.
// Init Algolia.
MainActivity.index = MainActivity.client.getIndex( "recsD" );
// Pre-build query.
query = new Query();
query.setFacets( "email:"+ FirebaseAuth.getInstance().getCurrentUser().getEmail().replace( ".","" ) );
//query.setFilters( "email:"+ FirebaseAuth.getInstance().getCurrentUser().getEmail().replace( ".","" ) );
query.setAttributesToRetrieve( "Note", "date", "time", "audio", "uid" );
query.setAttributesToHighlight( "Note" );
query.setHitsPerPage( HITS_PER_PAGE );
With the above code I'm able to fetch all results including the ones where email address doesn't match. What is it that I am not doing right? How can I achieve something like
SELECT * FROM recsD WHERE email='lilsaimo#gmailcom'

Your issue comes from using setFacets instead of setFilters. Quoting Algolia's documentation on facets:
Facets to retrieve. If not specified or empty, no facets are retrieved. The special value * may be used to retrieve all facets.
So using setFacets("foo", "bar") will ask the Algolia engine to return in the results the facet values for facet "foo" and "bar". This alone won't do any filtering!
If you want to filter results to only keep the ones matching foo=bar, you should:
Enable faceting on the foo attribute, either in the settings or for this query
Set the filter filters="foo:bar", as shown in the filters documentation under facet filters
Getting back to your android code, you should rather try:
// Pre-build query.
query = new Query();
query.setFacets("email");
query.setFilters( "email:"+ FirebaseAuth.getInstance().getCurrentUser().getEmail().replace( ".","" ) );

Related

Return documents based on field bitwise AND operator

I have a model which has a field 'permissions', and I would like to retrieve all the documents from my table whose field 'permissions' meets a certain condition based on a bitwise AND operator. For example, what I am doing currently is the following in my typescript codebase:
let users: User[] = await this.userModel.find();
users = users.filter( // I would like to get rid of this filter (do the filtering directly through mongo)
(user) => user.permissions & 2 > 0 // bitwise AND operator
);
However, I would like to do the operation directly through mongo, so I don't need to get all the users inside my table and then filter them out from my typescript code.

MongoDB Complex Query with Java

We have following structure in MongoDB documents.
{
"id":"1111",
"keys":[
{
"name":"Country",
"value":"USA"
},
{
"name":"City",
"value":"LongIsland"
},
{
"name":"State",
"value":"NewYork"
}
]
}
Now using Springframework Query object, I figured out a way to pull the details using below syntax
query.addCriteria(
Criteria.where("keys.value").is(countryparam).
andOperator(
Criteria.where("keys.value").is(stateparam)
)
);
Two issue with this query model.
First issue is it is irrelevant if countryparam and stateparam are actually meant to match Country key name and State key name respectively. If just the values matches, the query returns the document. Means, if I have Country and City params, this just works if user passes Country and City values, even if they are swapped. So how can I exactly compare City to cityparam and State to Stateparam?
More complexity is if I have to extract the document basing on multiple key value pairs, I should be correspondingly able to match key name with respective value and query the document. How can I do this?
Thanks in advance!

How to concat two columns for search with feathers-sequelize?

I need to search for users by name, their first name and last names are stored in separate columns in a postgresql database. The columns need to be concatenated for search to work properly. Typing the full first and last name of a user should match a result.
What could I pass as a query to the find method of a Feathers service that would allow me to do this?
As in the answer linked you can pass the where clause to the Feathers service by modifying params.query in a before hook:
app.service('users').before({
find(hook) {
const where = Sequelize.where(Sequelize.fn("concat",
Sequelize.col("firstname"),
Sequelize.col("lastname")), {
like: '%John Do%'
}
);
hook.params.query.where = where;
}
})

Exclude field from full-text search

I need to do the full text search in the MongoDB (version 2.4). I use the following fragment of code.
DBObject textSearchCommand = new BasicDBObject();
textSearchCommand.put("text", "profile");
textSearchCommand.put("search", pattern);
textSearchCommand.put("limit", searchLimit);
textSearchCommand.put("filter",new BasicDBObject("personInfo", new BasicDBObject("$ne",null)));
CommandResult commandResult = mongoTemplate.executeCommand(textSearchCommand);
BasicDBList results = (BasicDBList) commandResult.get("results");
It works well but I want to exclude one field (person picture data) from the text search.
Note: I don't want to exclude this field from the result. I want that MongoDB does not search in this field.
Which fields to search in is determined when you create the text index. When you only want the text index to apply to selected fields, you need to provide these fields at creation like this for example:
db.articles.createIndex(
{
title: "text",
synopsis: "text",
content: "text",
tags: "text"
}
)
When this is not an option for some reason (like when you don't know all possible field names which might be relevant for text search), an (admittedly dirty) workaround could be to store the non-searchable content in a different data-type than a string, for example as binary data.

MongoDB C# offic. List<BsonObject> query issue and always olds values?

I have not clearly issue during query using two criterials like Id and Other. I use a Repository storing some data like id,iso,value. I have created an index("_id","Iso") to performs queries but queries are only returning my cursor if i use only one criterial like _id, but is returning nothing if a use two (_id, Iso) (commented code).
Are the index affecting the response or the query method are failing?
use :v1.6.5 and C# official.
Sample.
//Getting Data
public List<BsonObject> Get_object(string ID, string Iso)
{
using (var helper = BsonHelper.Create())
{
//helper.Db.Repository.EnsureIndex("_Id","Iso");
var query = Query.EQ("_Id", ID);
//if (!String.IsNullOrEmpty(Iso))
// query = Query.And(query, Query.EQ("Iso", Iso));
var cursor = helper.Db.Repository.FindAs<BsonObject>(query);
return cursor.ToList();
}
}
Data:
{
"_id": "2345019",
"Iso": "UK",
"Data": "Some data"
}
After that I have Updated my data using Update.Set() methods. I can see the changed data using MongoView. The new data are correct but the query is always returning the sames olds values. To see these values i use a page that can eventually cached, but if add a timestamp at end are not changing anything, page is always returning the same olds data. Your comments are welcome, thanks.
I do not recall offhand how the C# driver creates indexes, but the shell command for creating an index is like this:
db.things.ensureIndex({j:1});
Notice the '1' which is like saying 'true'.
In your code, you have:
helper.Db.Repository.EnsureIndex("_Id","Iso");
Perhaps it should be:
helper.Db.Repository.EnsureIndex("_Id", 1);
helper.Db.Repository.EnsureIndex("Iso", 1);
It could also be related to the fact that you are creating indexes on "_Id" and the actual id field is called "_id" ... MongoDB is case sensitive.
Have a quick look through the index documentation: http://www.mongodb.org/display/DOCS/Indexes