I have a JSON with documents and each document has a few _childDocuments_ I want to search in the parent and get back the parents and facets on each parent separate
Here is a sample JSON
[{
"id": 1,
"productName": "Ford Explorer",
"_childDocuments_": [{
"id": 6,
"color": "blue",
"price": 1000
},
{
"id": 7,
"color": "red",
"price": 2000
}
]
},
{
"id": 1,
"productName": "Ford F150",
"_childDocuments_": [{
"id": 10,
"color": "blue",
"price": 5000
},
{
"id": 11,
"color": "red",
"price": 6000
}
]
},
{
"id": 2,
"productName": "Toyota Highlander",
"_childDocuments_": [{
"id": 8,
"color": "green",
"price": 1200
},
{
"id": 9,
"color": "red",
"price": 2000
}
]
}
]
I want when I search for Ford I should get back all Fords and in each root document get back facets of the children.
Something like this:
[{
"id": 1,
"productName": "Ford Explorer",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 2000,
"count": 1
},
{
"val": 3000,
"count": 1
}
]
}
}
},
{
"id": 1,
"productName": "Ford F150",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 5000,
"count": 1
},
{
"val": 6000,
"count": 1
}
]
}
}
}
]
This is what I got so far
q={!parent+which=type:parent}&json.facet={ "prices": {
"type": "range",
"field": "price",
"start": 1000,
"end": 1000,
"gap": 1000
}
,
}
But this is only returning facets on all child documents.
Thanks in advance
Related
unable to create sales tax on qbo api explorer.
i tried some ways but i can't create.
https://developer.intuit.com/app/developer/qbo/docs/develop/tutorials/manage-sales-tax-for-non-us-locales#overriding-sales-tax
{
"Line": [
{
"Description": "Pest Control Services",
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"TaxCodeRef": {
"value": "NON"
},
"Qty": 1,
"UnitPrice": 35,
"ItemRef": {
"name": "Pest Control",
"value": "10"
}
},
"LineNum": 1,
"Amount": 35.0,
"Id": "1"
}
]
}
I have the following document:
{
"month": 2,
"year": 2019,
"categories": [
{
"title": "Income",
"budget": 5000,
"amount": 2000,
"subcategories": [
{
"title": "Paychecks",
"budget": 4000,
"amount": 1000,
"transactions": [
{
"day": 3,
"amount": 600,
"vendor": "Microsoft",
"memo": "Paycheck 1"
},
{
"day": 10,
"amount": 400,
"vendor": "Google",
"memo": "Paycheck 2"
}
]
},
{
"title": "Other",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 7,
"amount": 600,
"vendor": "Mom",
"memo": "Gift for Baby"
},
{
"day": 19,
"amount": 400,
"vendor": "Amy",
"memo": "Gift for Julie"
}
]
}
]
},
{
"title": "Housing",
"budget": 3000,
"amount": 3000,
"subcategories": [
{
"title": "Rent",
"budget": 2000,
"amount": 2000,
"transactions": [
{
"day": 1,
"amount": 2000,
"vendor": "Homes4Rent",
"memo": "Rent"
}
]
},
{
"title": "Electric",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 14,
"amount": 1000,
"vendor": "Tampa Electric",
"memo": "Electric"
}
]
}
]
}
]}
I need to ensure that the "categories.budget" amount is always equal to the sum of the "categories.subcategories.budget" amounts for that category.
For example, I need to ensure that the budget for "Income" is 5000 because the sum of the budgets for its subcategories ("Paychecks" and "Other") is exactly 5000.
The most desirable solution would be one that can perform the sum and the "categories.budget" field update in a single query but I'm unsure how to accomplish this or if this is even possible. Here's what I've come up with so far:
db.collection.aggregate(
[
{
$match: {
month:2,
year:2019
}
},
{
$unwind: "$categories"
},
{
$unwind: "$categories.subcategories"
},
{
$group: {
_id: {
category: "$categories.title"
},
sum: {
$sum: "$categories.subcategories.budget"
}
}
}
])
Unfortunately, this only seems to be printing out the category budgets instead of summing them. Whichever solution is realized will ultimately be used in various places throughout my application to perform similar operations as well.
Thank you so much and let me know if I need to provide any further information.
I'm currently working on a search using elasticsearch. We have a very large amount of users.
Here is the elasticsearch mapping:
PUT /example_index/_mapping/users
{
"properties": {
"user_autocomplete": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"completion": {
"type": "text",
"analyzer": "user_autocomplete_analyzer",
"search_analyzer": "standard"
}
}
},
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
}
}
}
Here is the search query.
For example, I get 3 records
GET example_index/users/_search
{
"from": 0,
"size": 3,
"query": {
"query_string": {
"query": "*ro*",
"fields": [
"firstName",
"lastName"
]
}
},
"aggs": {
"user_suggestions": {
"terms": {
"size": 3,
"field": "user_autocomplete.raw"
}
}
}
}
Here is the output of elasticsearch
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 13,
"max_score": 1,
"hits": [
{
"_index": "example_index",
"_type": "users",
"_id": "08",
"_score": 1,
"_source": {
"firstName": "Eero",
"lastName": "Saarinen",
"user_autocomplete": "Eero Saarinen"
}
},
{
"_index": "example_index",
"_type": "users",
"_id": "16",
"_score": 1,
"_source": {
"firstName": "Aaron",
"lastName": "Judge",
"user_autocomplete": "Aaron Judge"
}
},
{
"_index": "example_index",
"_type": "users",
"_id": "20",
"_score": 1,
"_source": {
"firstName": "Robert",
"lastName": "Langdon",
"user_autocomplete": "Robert Langdon"
}
}
]
},
"aggregations": {
"user_suggestions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 10,
"buckets": [
{
"key": "Eero Saarinen",
"doc_count": 1
},
{
"key": "Aaron Judge",
"doc_count": 1
},
{
"key": "Robert Langdon",
"doc_count": 1
}
]
}
}
}
I need result like in the following order:
Robert Langdon
Aaron Judge
Eero Saarinen
I have tried order method. It won't work. Is there a way to do this?
How to create angular ui tree for complex json?
[
{
"name": "bmw",
"model": "x3",
"specs": [
{
"name": "bmw",
"maxspeed": 350,
"minspeed": 0,
"model": "x3",
"type": "car"
},
{
"value": 30,
"name": "bmw",
"maxspeed": 350,
"minspeed": 0,
"model": "x3",
"type": "car"
}
],
"available": true,
"price": 50
},
{
"name": "audi",
"model": "r8",
"specs": [
{
"color": [
{
"value": "red",
"price": 100
},
{
"value": [
"white",
"black"
],
"price": 200
}
],
"maxspeed": 330,
"minspeed": 0,
"type": "car"
},
{
"color": [
{
"value": "blue",
"price": 300
},
{
"value": [
"yellow",
"gold"
],
"price": 500
}
],
"maxspeed": 330,
"minspeed": 0,
"type": "car"
}
],
"available": true
}
]
I have users indexed with categories as follows
{
id: 1
name: John
categories: [
{
id: 1
name: Category 1
},
{
id: 2
name: Category 2
}
]
},
{
id: 2
name: Mark
categories: [
{
id: 1
name: Category 1
}
]
}
And I'm trying to get all the documents with Category 1 or Category 2 with
{
filter:
{
bool: {
must: [
{
terms: {user.categories.id: [1, 2]}
}
]
}
}
}
But It only returns the first document that has the two categories, what I am doing wrong?
As I understood, terms search that one of the values is contained in the field, so for user 1
user.categories.id: [1, 2]
user 2
user.categories.id: [1]
Categoy id 1 is contained in both documents
The best way to handle this is probably with a nested filter. You'll have to specify the "nested" type in your mapping, though.
I can set up an index like this:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"doc": {
"properties": {
"categories": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
},
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
}
}
}
then add some docs:
PUT /test_index/doc/1
{
"id": 1,
"name": "John",
"categories": [
{ "id": 1, "name": "Category 1" },
{ "id": 2, "name": "Category 2" }
]
}
PUT /test_index/doc/2
{
"id": 2,
"name": "Mark",
"categories": [
{ "id": 1, "name": "Category 1" }
]
}
PUT /test_index/doc/3
{
"id": 3,
"name": "Bill",
"categories": [
{ "id": 3, "name": "Category 3" },
{ "id": 4, "name": "Category 4" }
]
}
Now I can use a nested terms filter like this:
POST /test_index/doc/_search
{
"query": {
"constant_score": {
"filter": {
"nested": {
"path": "categories",
"filter": {
"terms": {
"categories.id": [1, 2]
}
}
}
},
"boost": 1.2
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "John",
"categories": [
{
"id": 1,
"name": "Category 1"
},
{
"id": 2,
"name": "Category 2"
}
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"name": "Mark",
"categories": [
{
"id": 1,
"name": "Category 1"
}
]
}
}
]
}
}
Here is the code I used:
http://sense.qbox.io/gist/668aefe910643b52a3a10d40aca67104491668fc