Algolia: search for multiples values in each fields with AND clause - javascript

I would like know if i can query different values in each field
returning values that only match with the search like AND condition. I tried use search in multpile indexes, but it return values like OR condition.
Example:
my index contains:
[
{
firstName: 'Alisson',
lastName: 'Oliveira',
},
{
firstName: 'Alex',
lastName: 'Oliver',
},
{
firstName: 'Daniel',
lastName: 'Costa',
}
]
my query would be:
index.search(query: { firstName: 'Al', lastName: 'Oliv' })
the response espected:
[
{
firstName: 'Alisson',
lastName: 'Oliveira',
},
{
firstName: 'Alex',
lastName: 'Oliver',
}
]
Someone know if is possible in the algolia?

Based on your example it sounds like your end-user would type the query: "Al Oliv" and you would expect the two records shown above to return.
To do this, you would change the default settings from prefixLast to prefixAll. You can do this in the Dashboard:
By default Algolia only uses the last word as a prefix. If you make the change above the query will return the expected results:

Related

How to remove an object from an array if it has the letter a?

I need to make a function that receives 2 parameters, the first one is an array of a list of users that must contain first name, last name and phone numbers, at least 3 of those users must start with the letter "a", the second parameter is a callback. You must process the array and delete all the users whose name starts with the letter "a". Then send the new processed array to the callback. The callback must show in console, the list of all the names, concatenating first and last name.
const users =
[{
name: 'Diego',
lastname: 'Garcia',
phone: '12343'
},
{
name: 'Camilo',
lastname: 'Garcia',
phone: '12343'
}, {
name: 'ana',
lastname: 'Rodriguez',
phone: '02343'
}, {
name: 'anastasia',
lastname: 'Zapata',
phone: '42343'
}, {
name: 'alejandra',
lastname: 'Perez',
phone: '52343'
}];
const operation2 = (list) => {
let x = [];
let callback = {};
callback = list.find(element => element.name.charAt(0) == 'a');
let l = list.indexOf(callback)
let g = list[l];
console.log(g)
if (callback) {
x = list.splice(l, 1)
} return list
}
console.log(operation2(users))
The code that I made does not work, it is not eliminating all the names that begin with "a", it only eliminates the first object of the array.
Array.find only finds the first match, you should probably filter out the elements you don't want:
const result = list.filter(element => element.name.charAt(0) !== 'a');
const users =
[{
name: 'Diego',
lastname: 'Garcia',
phone: '12343'
},
{
name: 'Camilo',
lastname: 'Garcia',
phone: '12343'
}, {
name: 'ana',
lastname: 'Rodriguez',
phone: '02343'
}, {
name: 'anastasia',
lastname: 'Zapata',
phone: '42343'
}, {
name: 'alejandra',
lastname: 'Perez',
phone: '52343'
}];
function func(list, callback) {
let users = list.filter(u => !u.name.toLowerCase().startsWith('a'));
callback(users)
}
func(users, (users) => {
console.log(users)
})

Gatsby createResolvers for data types within other data types

I've got this data set:
const avengers = [
{
firstName: 'Tony',
lastName: 'Stark',
name: 'Iron Man',
bestFriends: {
name: 'Captain America',
},
},
{
firstName: 'Bruce',
lastName: 'Banner',
name: 'Hulk',
bestFriends: {
name: 'Black Widow',
},
},
{
firstName: 'Thor',
lastName: 'Odinson',
name: 'Thor',
bestFriends: {
name: 'Rocket',
},
},
]
and what I'm trying to accomplish in Gatsby is to extend the bestFriends node in my Gatsby Nodes and allow the bestFriend's data to be fetched via a single query. My result query would look like this:
query myQuery {
Avenger (name: {eq: "Iron Man}) {
name
firstName
lastName
bestFriends {
name
friendData {
firstName
lastName
name
bestFriends
}
}
}
}
However I'm struggling to figure out how to set up my createResolvers so that I can set a custom resolver function for the friendData node. It almost seems as if the Gatsby createResolvers API isn't able to resolve nested data sets. I assumed that this should work but when I do query the data via GraphiQL the node bestFriends returns "null".
createResolvers({
Avenger: {
bestFriends: {
type: "Avenger",
resolve: (source, args, context) => {
return context.nodeModel.runQuery({
query: {
filter: {
name: {eq: source.bestFriends.name}
}
}
})
}
}
}
})
};
Any idea how I can accomplish what I'm getting at? I imagine that this is how Facebook would set up data structures for posts and comments, where the commentor's name, profile url, profile picture, and the comment itself is visible when you look at a post.

In JavaScript, how can I store an array of object value into string variable?

I have this object with the following data that I need to store in a variable:
user: [
{
firstname: 'John',
lastname: 'Doe',
roles: [
{
roleId: 18,
displayText: 'accountant',
description: 'accountant'
}
]
}
]
I am looping through this object as such:
let selectedRole = user.roles.map((role, index) => {
return role.displayText
})
selectedRole returns me the following output: ["accountant"]
For example:
John Doe has only 1 role assigned to him -> account
Sam Williams has 3 roles assigned -> program manager, account, admin
let's say if I click on Sam William's details then I want to see all the roles that I assigned to him in not comma separated but in the following layout:
Role 1: program manager
Role 2: account
Role 3: admin
If you want to get all elements of the array as a delimited string, you can use Array.prototype.join: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
let selectedRoles = user.roles.map((role, index) => {
return role.displayText
});
let rolesString = selectedRoles.join();
If selectedRoles is ['accountant', 'admin'] then the value of rolesString will be accountant, admin. The parameter of join is optional, but its value is the delimiter (comma is default).
EDIT
So if you don't want them as one string, but instead you want to display them individually on the screen. How are you displaying them? Are you using Angular or some other library/framework, or are you using plain JavaScript?
If you're using Angular, for example, you'll use its mechanisms for looping over a collection. So:
<ul *ngFor="let role of selectedRoles; let idx = index">
<li>Role {{idx}}: {{role}}</li>
</ul>
If you don't need an array, but just the string, then don't use .map:
const user = [
{
firstname: 'John',
lastname: 'Doe',
roles: [
{
roleId: 18,
displayText: 'accountant',
description: 'accountant'
}
]
}
];
const selectedRole = user[0].roles[0].displayText;
console.log(selectedRole);
If you want to join all roles into a string, use .join by whatever delimiter you want:
const user = [
{
firstname: 'John',
lastname: 'Doe',
roles: [
{
roleId: 18,
displayText: 'accountant',
description: 'accountant'
},
{
roleId: 20,
displayText: 'somethingElse',
description: 'somethingElse'
}
]
}
];
const selectedRole = user[0].roles.map((r, i) => `Role ${i + 1}: ${r.displayText}`).join('\n');
console.log(selectedRole);

ajax-like search with internal list

I apologize ahead of time if the title was worded oddly!
I want to do an ajax-like search on a list I have on the client side (i.e. a list of friends). The list would look like this:
var friendList = new Array [
{
name: "Mark",
email: "mark#gmail.com",
birthDate: "01/01/1980"
},
{
name: "Steve",
email: "steve#gmail.com",
birthdate: "02/02/1980"
},
{
name: "Frank",
email: "frank#gmail.com",
birthDate: "03/03/1980"
}
]
How would I search letter by letter with an input field like so:
<input type="search" id="searchFriends" />
Thanks!

error in a search function in javascript

I am trying to return contact info for "steve", but codecademy is telling me that it is not returning correctly.
It looks like your search function doesn't return contact information for Steve.
I am not able to find any error in this code. Can you help me with finding any syntax or logical errors in the search function that you can see?
var friends = {
bill: {
firstName: "bill",
lastName: "gates",
number :21415,
address: ["mcklsn", "wcnskln"]
},
Steve: {
firstName: "bill",
lastName: "gates",
number: 21415,
address: ["mcklsn", "wcnskln"]
}
};
var list = function (friends)
{
for (bill in friends)
console.log(bill);
for (steve in friends)
console.log(steve);
};
var search = function(name)
{
for (var i in friends)
{
if (friends[i].firstName === name)
{
console.log(friends[i]);
return friends[i];
}
else
console.log("contact doesn't exist!");
}
};
You should really pay attention to what you are writing, don't Ctrl-C Ctrl-V everything blindly. You are not calling your friends in the correct names and I would be offended if someone did that to me. Heck, Steve Jobs would be furious if you called him Bill!
This should work:
var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: 21415,
address: ["mcklsn", "wcnskln"]
},
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: 21416,
address: ["mcklsnn", "wcnsklnn"]
}
};
search("Bill");
search("Steve");
Yah, because your Steve property object's firstName is set to "bill".
So that code, searching for a match to 'steve' on the firstName property will not find one...because there isn't one. Both of those object's have 'bill' as their firstName property.

Categories

Resources