How to use elasticsearch query with searchUsers function in Kuzzle? - node.js

i set a property called type in some users with the kuzzle console, now i want to search for user who have the type set to user so i use a query for searching user.
Here is my code:
const resultUsers = await kuzzle.security.searchUsers({
query: {
term: {
type: "user"
}
}
})
console.log(resultUsers)
I also tried with this query too:
query: {
term: {
"content.type": "user"
}
}
and this one:
query: {
term: {
"_source.type": "user"
}
}
But the function always return 0 users. Can someone explain me why please ?

You can only search for properties that have been properly indexed.
You can check the mappings of the internal users collection with the security:getUserMapping API action or with the Admin Console.
Then you need to modify the mappings to include your new property:
{
"properties": {
"type": { "keyword" }
}
}
For this, you can use the security:updateUserMapping API action or again, the Admin Console.

Related

Get soft deleted records using Nestjs and TypeOrmQueryService

I am trying to get soft deleted records (deletedAt column) when using query from TypeOrmQueryService but looks like there is no way to do that. I know that I can use withDeleted if I use find or findOne but I cannot switch to these methods or use a query builder since it would require a lot of changed in the front-end.
#QueryService(Patient)
export class PatientQueryService extends TypeOrmQueryService<Patient> {
constructor(#InjectRepository(Patient) repo: Repository<Patient>) {
super(repo);
}
async getOnePatient(currentUser: User, patientId: number) {
const result = await super.query({
paging: { limit: 1 },
filter: { id: { eq: 1 } },
});
}
}

How can I automatically append a property from a relation to the root object?

How can I automatically append a property from a relation to the root object, as if it were a column from the same table but actually it is coming from another table.
Supose I have an User model that hasMany Emails.
How can I only append the email from the first Email of the User model, so that everytime I query the User model I get it like a property?
Example:
What I'm doing:
(await User.query().where('id', id).with('emails').first()).toJSON()
{
"name": "Eleandro Duzentos",
"emails": [
{ "email": "eleandro#inbox.ru" },
{ "email": "eleandro#mail.ru" }
]
}
What I want:
(await User.find(id)).toJSON()
{
"name": "Eleandro Duzentos",
"email": "eleandro#inbox.ru"
}
Obs: I'm not putting the email on the same table because, there's a chance that a user may need more then one email in a long future, but for now, it has only one.
How can I do that?
For the customized JSON response i would suggest the use of serializers.
You can override the default serializers to get the desired result.
You can refer to this - https://adonisjs.com/docs/4.0/serializers
Here is my code. You could be inspired by it:
Model User:
...
const Email = use('App/Models/Email')
class User extends Model {
async getEmails() {
let list = []
let emails = await Email.query().where('user_id', this.id).fetch()
emails.rows.forEach(email => {
list.push({ name: this.username, email: email.email })
});
return list
}
emails() {
return this.hasMany('App/Models/Email')
}
}
module.exports = User
Controller :
...
let user = await User.find(1)
return await user.getEmails()
Output :
[
{"name":"CrBast","email":"test#crbast.ch"},
{"name":"CrBast","email":"test2#crbast.ch"}
]
Feel free to correct me if that's not what you want :)

How to use Github GraphQL search to return user profiles with similar name( including login and display name)?

I have this simple search query
query test($name: String!) {
search(query: $name, type: USER, last: 100) {
edges {
textMatches {
fragment
property
highlights {
text
}
}
}
userCount
}
}
and say, for example, I would like to have the login information for all users from the search result. How would I do that? The results contain login or display names that matches the search text. Is there a way to find the login for those who only appear in the search because of their display name?
You were almost there! In "edges", you're dealing with an array of SearchResultItemEdge which contains a "node" property at the same level as "textMatches".
Since the node is a SearchResultItem, and can be one of User, Issue, PullRequest, etc, you have to specifically spread your node as a "User" in order to be able to access the login.
Give this query a try in the Explorer:
query test($name: String!) {
search(query: $name, type: USER, last: 100) {
edges {
node {
__typename
...on User {
login
}
}
textMatches {
fragment
property
highlights {
text
}
}
}
userCount
}
}

Cursor Pagination with Apollo/GraphQL keeps giving me error

I'm trying to implement cursor pagination and followed the examples in the doc but I keep getting an error saying 'Cannot query field "cursor" on type "Query"'.
I'm aware that the "cursor" field doesn't actually exist on the Accounts schema...but from what I'm reading from the docs.. you have to include it somewhere in the gql`` query. Furthermore, not sure if I'm missing anything but I'm a bit confused of how to structure my query to allow cursor pagination.
Original Query: (running this gives me no error)
const AccountsQuery = gql`
query {
accounts {
accountName
accountId
}
}
`;
New Query: (this gives "cannot find cursor field on accounts" error)
const AccountsQuery = gql`
query Accounts($cursor: String){
accounts(cursor: $cursor) {
cursor
accountName
accountId
}
}
`;
GraphQL wrapper:
export default graphql(AccountsQuery, {
props: ({ data: { loading, cursor, accounts, fetchmore } }) => ({
loading,
accounts,
loadMoreEntries() {
return fetchmore({
query: AccountsQuery,
variables: {
cursor: cursor,
},
updateQuery: (previousResult, { fetchMoreResult }) => {
const previousEntry = previousResult.entry;
const newAccounts = fetchMoreResult.accounts;
return {
cursor: fetchMoreResult.cursor,
entry: {
accounts: [...newAccounts, ...previousEntry]
},
};
},
})
}
})
})(QuickViewContainer);
Any help would be appreciated to getting cursor pagination working!
Sounds like the cursor field isn't getting implemented on the server. Your Account type needs to have that field like so:
Account {
cursor
accountName
accountId
}
For a convention on how to do cursor pagination, you should follow the standard Relay spec. You can read more about how it's implemented here in a Relay-compliant GraphQL API.
This would make your query look like this:
query {
viewer {
allAccounts {
edges {
cursor
node {
accountName
accountId
}
}
}
}
}
Each edge account has a cursor that corresponds to a node and will be auto-populated with globally-unique opaque cursor IDs from the server.
Hope this helps!

nodeJS loopback include filter

i have a problem with Node JS loop-back include filter when i make a query .Every time its giving all records in response.but i want only those records that holds the sportid which i am passing in query filter.
var request = {
method: 'get',
command: 'UserPersonalinfos',
query: {
filter: {
"include": {
"relation": "UserRegistration",
"where": {
"sportid": data[0].id
}
},
limit:5
}
},
headers: {
access_token:userAccessToken.id,
}
};
if sportid exists then it should return object otherwise it should not, but in this case its returns all UserPersonalinfos records and adding UserRegistration object to UserPersonalinfos object
As per https://stackoverflow.com/a/32933383/344022 there's nothing officially available yet. There is a fork of the loopback-connector by #DiogoDoreto that does attempt to provide it. I haven't tried it, but if you were to use it you would do the following in a filter:
"where": {
"UserRegistration": {
"where": {
"sportid": data[0].id
}
}
}
The answer linked above also suggests some other ways of achieving what you want.

Resources