I would like to get the opposite GraphQL query field:
which mean the query string will not appeal in the result, while not inside the query string will appeal in the result.
Because there are varties of json recorders, I can not manually write opposite query. Any way to write opposite query automatically?
for example, I have JSON like:
{
"data": {
"source": "AWS",
"hero": {
"version": "my version",
"name": "R2-D2",
"friends": [
{
"attribute": "like something",
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
I have query
{
source,
hero {
name
friends {
attribute
}
}
}
I like to get a result:
{
"data": {
"hero": {
"version": "my version",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
Which the query fields not in the query will appear in the result, while the query fields in the query will not inside the result.
How to do these operations in JavaScript? Can you give me an example?
You only get the fields you include in the Query if you want to get whats not in the query then write another query for the fields not in the original query.
At its simplest, GraphQL is about asking for specific fields on objects.
https://graphql.org/learn/queries/
Query:
{
hero {
version
friends {
name
}
}
}
Result:
{
"data": {
"hero": {
"version": "my version",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
Update from comments:
Your question is more of how to Dynamically Generate GraphQL Queries?
In this case you could use fragments, but you would still have to write multiple queries.
Fragments let you construct sets of fields, and then include them in queries where you need to.
https://graphql.org/learn/queries/#fragments
Related
Given the ElasticSearch document below:
{
"_id": "3330481",
"_type": "user",
"_source": {
"id": "3330481",
"project": "Cool_Project_One"
}
}
I'm building a UI component that will auto suggest to the user all the values in "project" field base on his text input
For example:
As the user types "Cool" i would like to show him all the values from the "project" field that starts with "Cool"
I've create this aggregation:
"aggs": {
"projects": {
"terms": {
"field": "project",
"size": 2
}
}
}
which returns me a list with all the values for the project field, but i can't understand how should i find only the values that are matching to a certain expression.
I've found this answer that shows how to add filter, but it seems that the filter returns only exact matches as i tried to do that:
{
"aggs": {
"projects": {
"filter": {
"term": {
"project": "Cool"
}
},
"aggs": {
"terms": {
"field": "project",
"size": 2
}
}
}
}
}
And it didn't worked.
Any help would be appreciated
Some notes:
term query will look for exact matches (casing included, if you want something more flexible you can use a regular "match" query
In general, you want to add the filter in the query, no aggregations
You can use aggregations to group by category type fields, but you can use regular queries to match against title type fields
I would suggest to use a suggestion field type for this, to also capture prefixes (proj in project por example).
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html
I just seen you have _type defined, so probably you are using an old version of Elasticsearch, and search_as_you_type is relatively new. I will add some examples with both query and aggs:
Mappings for search_as_you_type, text, and keyword fields :
PUT test_suggestions
{
"mappings": {
"properties": {
"project": {
"type": "text",
"fields": {
"suggestions": {
"type": "search_as_you_type"
},
"keyword": {
"type": "keyword"
}
}
}
}
}
}
Indexing document
POST test_suggestions/_doc
{
"project": "Cool Project"
}
search_as_you_type query, supports prefixes out of the box:
GET test_suggestions/_search
{
"query": {
"multi_match": {
"query": "coo",
"type": "bool_prefix",
"fields": [
"project",
"project._2gram",
"project._3gram"
]
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
Regular query, case insensitive and you can write just a portion of the field and will work
GET test_suggestions/_search
{
"query": {
"match": {
"project": "Cool"
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
Bonus: prefix search without search_as_you_type or setting up ngrams:
GET test_suggestions/_search
{
"query": {
"match_phrase_prefix": {
"project": "coo"
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
I am using NODE JS with elastic search DB .
I am using this package
https://www.npmjs.com/package/#elastic/elasticsearch
I have this collection in my elastic search DB
[
{
"_index": "products",
"_id": "wZRh3n8Bs9qQzO6fvTTS",
"_score": 1.0,
"_source": {
"title": "laptop issues",
"description": "laptop have issue present in according"
}
},
{
"_index": "products",
"_id": "wpRh3n8Bs9qQzO6fvzQM",
"_score": 1.0,
"_source": {
"title": "buy mobile",
"description": "mobile is in Rs 250"
}
},
{
"_index": "products",
"_id": "w5Rh3n8Bs9qQzO6fvzTz",
"_score": 1.0,
"_source": {
"title": "laptop payment",
"description": "laptop payment is given in any way"
}
}
]
now I am planning to fetch data from elastic DB . when I am passing "LAP" or "lap" . it is giving me blank array or [] array why ? "lap" is present in all object
I am doing like that
const result= await client.search({
index: 'products',
query: {
match_phrase: {
description: "lap"
}
}
where I am doing wrong . I need all result where lap keywords is present
Match query is not working because you are trying to search partial character of laptop term.
You can use Prefix Query for single term like below:
{
"query": {
"prefix": {
"title": {
"value": "lap"
}
}
}
}
If you want to search for phrase then you can use Phrase Prefix Query:
{
"query": {
"match_phrase_prefix": {
"title": "lap"
}
}
}
If you want to match only some of the word from query then you can use match query with operator set to or.
POST querycheck/_search
{
"query": {
"match": {
"title": {
"query": "i have issues",
"operator": "or"
}
}
}
}
Please, help me. I cant find information about how do this.
I have got this code. It load all products with all relations. One of relations is product item. In product item entity I have got price column.
How I can get minimal product item price without get in my response array of product items?
const { skip, take } = pagination;
const query = this.createQueryBuilder('product');
query.where('product.shop = :id AND product.blocked = FALSE', {
id: shop.id,
});
if (skip) {
query.offset(Number(skip));
}
if (take) {
query.limit(Number(take));
}
query.leftJoin('product.info', 'info');
query.leftJoin('product.avatar', 'avatar');
// load product items
query.leftJoin('product.productItem', 'item');
query.select([
'product.id',
'product.path',
'info.name',
'info.description',
'info.info',
'info.lang',
'avatar.path',
'avatar.type',
'item.price'
]);
const [list, amount] = await query.getManyAndCount();
Now i have got:
{
"list": [
{
"id": 3,
"path": "admin-product-2",
"order": 1,
"shop": {
"id": 1
},
"info": [
{
"name": "Admin Name ;)",
"description": "Shorty",
"info": "",
"lang": "RU"
}
],
"avatar": null,
"productItem": [
{
"price": 1000
},
{
"price": 500
},
{
"price": 300
},
{
"price": 2000
},
{
"price": 3000
}
]
}
]
}
........
But I need:
{
"list": [
{
"id": 3,
"path": "admin-product-2",
"order": 1,
"shop": {
"id": 1
},
"info": [
{
"name": "Admin Name ;)",
"description": "Shorty",
"info": "",
"lang": "RU"
}
],
"avatar": null,
"minProductItemPrice": 300
}
]
}
Pls help me
You can find the answer for this on Stackoverflow already.
Here is a similar question Typeorm select max with specific column
Basically, getManyAndCount() method that you are using is useful when fetching entities. In your case, you are trying to obtain an aggregate value encompassing multiple entities.
You need to make separate selection, like so
query.select("MIN(item.price)", "min");
and then get the result with
return query.getRawOne();
I'm working with the structure below for a multilanguage site. For this I'll be doing a tool to add/edit the needed words in the different languages that'll be available.
What I want to do is use a MongoDB (or Mongoose) query on my NodeJS/Express backend to get the keys for a specific language-
{
"languages": [
{
"_id": "5d4ee75c1c9d4400007e9bd8",
"code": "en",
"texts": [
{
"key": "language",
"word": "English"
},
{
"key": "title",
"word": "Colorblindness"
},
{
"key": "login",
"word": "Login"
}
]
},
{
"_id": "5d4ee8631c9d4400007e9bd9",
"code": "es",
"texts": [
{
"key": "language",
"word": "EspaƱol"
},
{
"key": "title",
"word": "Daltonismo"
},
{
"key": "account",
"word": "Cuenta"
},
{
"key": "exit",
"word": "Salir"
}
]
},
{ ... }
]
}
For example, if I query on "en" (English language), I'd expect the result to be something like
["language", "title", "login"]
, but if I query on "es" (Spanish language), the result should be
["language", "title", "account", "exit"]
Schema.findOne({code: "en"},(err,res)=>{
if (res) console.log(res.texts);
});
It's mongoose, it won't work with it's current but you need to edit it depending on your code starting by schema name.
Assuming every code has it's own fields, you should change "en" to "es".
For your query, assuming languages.code has unique values :
db.yourCollectionName.aggregate([
{$match:{'languages.code': "es"}},
{$unwind:'$languages'},
{$match:{'languages.code': "es"}},
{$project:{keys:'$languages.texts.key'}}
])
Assuming your collection is a large dataset then adding $match as first stage would filter documents where at-least one element of languages.code has value 'es'.
i have the json below:
{
"data": [
{
"name": "product1",
"details": ["lorem ipsum", [
{
"code": "prd1"
},
{
"code": "prd11"
}]
]
},
{
"name": "product2",
"details": ["lorem ipsum", [
{
"code": "prd2"
},
{
"code": "prd22"
}]
]
}
]
}
and i want to retrieve the name of the product based on the code, so
i wrote this query
$.data..[?(#.code=="prd1")]
Result:
[
{
"code": "prd1"
}]
Expected result:
[{name: "product1"}]
You should move up the tree until you reach the value of a specific key. At first, we need to reach the first named ancestor, which is details, then - to get its first parent, which holds the needed name property. This can be done using ancestor() function and parent() function:
$.data..[?(#.code=="prd1")].ansector("details").parent()['name'];