elasticsearch multi-index get request does not work - lucene

How can I use GET api on multiple indexes?
I tried the following but I keep getting index missing exception.
http://localhost:9200/index1,index2/_all/AUy25vKhcC3G2n2ukra3
Output:
{
"error" : "IndexMissingException[[index1,index2] missing]",
"status" : 404
}
Please help

Use multi-get api
Example:
POST test1/index1/1
{
"name": "jon brick"
}
POST test2/index1/1
{
"name": "sriji"
}
GET _mget
{
"docs" : [
{
"_index": "test1",
"_type":"index1",
"_id" : "1"
},
{
"_index": "test2",
"_type":"index1",
"_id" : "1"
}
]
}
and the result is:
{
"docs": [
{
"_index": "test1",
"_type": "index1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "jon brick"
}
},
{
"_index": "test2",
"_type": "index1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "sriji"
}
}
]
}

Related

Shopware 6 API / Product Listing / How to add available color variants for each product on the response

API Request
/store-api/product-listing/{categoryId}
I want each product in the API response to have available color variants. Because I want to show color variants on the product list page. But currently this data is not provided by the shopware 6 api.
I've tried to add this data by using associations but it didnt help.
Request Body
{
"includes": {
"product": ["id", "productNumber", "cover", "options"]
},
"associations": {
"options": {
"media": []
}
}
}
The request body looks to be correct. If the product is a variant then it should have content for options.
To verify try the following request body:
{
"limit": 1,
"includes": {
"product": ["id", "productNumber", "options"],
"property_group_option": ["name", "group"],
"property_group": ["name"]
},
"associations": {
"options": {
"associations": {
"group": []
}
}
},
"filters": [
{
"type": "not",
"operator": "and",
"queries": [
{
"type": "equals",
"field": "parentId",
"value": null
}
]
}
]
}
This should yield a single variant.
Response should look similar to this:
{
"entity": "product",
"total": 1,
"aggregations": [],
"page": 1,
"limit": 1,
"elements": [
{
"productNumber": "10042.1",
"options": [
{
"name": "linen",
"group": {
"name": "textile",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
},
{
"name": "35",
"group": {
"name": "size",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
},
{
"name": "chartreuse",
"group": {
"name": "shirt-color",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
}
],
"id": "0002ea44c49c41ecb91c43e7e49e422d",
"apiAlias": "product"
}
],
"states": [],
"apiAlias": "dal_entity_search_result"
}

pass value from different Collection inside subquery in Match operation in Mongodb

I have 2 collection: Company and Stores. I used $lookup on Store collection using the storeIds on Company collection. I need to pass 'Company.Year' value to Stores.Statuses.Year inside $Match operator. Any help is appreciated.
Company: [
{
"_id": {
"$oid": "1388445c0000000000000001"
},
"name": "Company A",
"year": 2022,
"storeIds": [
{
"$oid": "1388445c0000000000000011"
},
{
"$oid": "1388445c0000000000000012"
}
]
}
]
Store: [
{
"_id": {
"$oid": "1388445c0000000000000011"
},
"name": "Store A",
"statuses": [
{
"year": 2021,
"status": "pending"
},
{
"year": 2022,
"status": "review"
}
]
}
]
Output: [
{
"_id": {
"$oid": "1388445c0000000000000001"
},
"name": "Company A",
"year": 2022,
"storeIds": [
{
"$oid": "1388445c0000000000000011"
}
],
"statuses": [
{
"year": 2022,--->How to pass company.year value instead of hard-coded value(2022).
"status": "review"
}
]
}
]

aggregate in mongodb left join with $lookup

I have three collections
posts=[
{
"id": "p1",
"title": "title 1"
},
{
"id": "p2",
"title": "title 2"
}]
users = [
{
"id": "u1",
"name": "name1"
},
{
"id": "u2",
"name": "name2"
}]
comments = [
{
"userId": "u1",
"postId": "p1",
"comment": "comment 1"
}]
I want to get all collection posts and comments in each post by userId(u1) as:
posts=[
{
"id": "p1",
"title": "title 1",
"comments":[
"userId": "u1",
"comment": "comment 1"
]
},
{
"id": "p2",
"title": "title 2",
"comments":[]
}]
I used aggregate function and $lookup operator but I don't know using the $match operator to filter userId. I used aggregate bellow:
self.db.posts.aggregate([
{
"$lookup":{
"from": "comments",
"localField": "id",
"foreignField": "postId",
"as": "comments",
}
},
{
"$match":{
"comments.userId": {"$eq": param.objectUserId}
},
},
{"$skip": (param.page - 1) * param.pageSize},
{"$limit": param.pageSize},
{"$sort": {"unixDate": pymongo.DESCENDING}}
])
It only return one post in array corresponding with userId="u1"
Please help me!
Thank all!
You have to make use of the pipeline option of $lookup stage and pass the additional conditions that you want to apply.
db.posts.aggregate([
{
"$lookup": {
"from": "comments",
"let": {
"pId": "$id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$postId",
"$$pId"
],
},
"userId": "u1",
},
},
{
"$project": {
"_id": 0,
"userId": 1,
"comment": 1,
},
},
],
"as": "comments"
}
}
])
Mongo Playground Sample Execution
self.db.posts.aggregate([
{
"$lookup": {
"from": "comments",
"let": {
"pId": "$id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$postId",
"$$pId"
],
},
"userId": param.objectUserId,
},
},
{
"$project": {
"_id": 0,
"userId": 1,
"comment": 1,
},
},
],
"as": "comments"
}
},
{"$skip": (param.page - 1) * param.pageSize},
{"$limit": param.pageSize},
{"$sort": {"unixDate": pymongo.DESCENDING}}
])

Creating a Mango query selector for element that do NOT match

I have a user like so:
{
"_id": "u88888",
"_rev": "27-9f205fb7496645be2f3d717fff376d73",
"name": "Test Testerson",
"type": "user",
"role": "boss",
"training": [
{
"id": "11111",
"name": "training 1"
},
{
"id": "33333",
"name": "training 3"
}
]
}
I'm trying to run a query to find the following:
Is a type, "user"
Has a role of "boss"
Does not have the training of id: "11111"
I do the following selector, however, this user comes up and I'm not sure why:
selector:{
"$and": [
{ "type": "user" },
{ "role": "boss" },
{ "training": { "$ne": { "id": "11111" } } }
]
}
Found an answer. This ended up working:
{ training: { $not: {$elemMatch: {id: "11111" } } }

Query the latest document of each type on Elasticsearch

I'm trying to run what started to look like a simple query on Elasticsearch, but I just can't seem to get the result I'm looking for.
Here's a brief example of what I'm trying to do:
I have a database of news. Each piece of news contains a source, a headline, a timestamp and a user.
I want the get the last (timestamp based) headline for each available source for a given user.
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/news" -d '{
"mappings": {
"news": {
"properties": {
"source": { "type": "string", "index": "not_analyzed" },
"headline": { "type": "object" },
"timestamp": { "type": "date", "format": "date_hour_minute_second_millis" },
"user": { "type": "string", "index": "not_analyzed" }
}
}
}
}'
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"news","_type":"news"}}
{"user": "John", "source": "CNN", "headline": "Great news", "timestamp": "2015-07-28T00:07:29.000"}
{"index":{"_index":"news","_type":"news"}}
{"user": "John", "source": "CNN", "headline": "More great news", "timestamp": "2015-07-28T00:08:23.000"}
{"index":{"_index":"news","_type":"news"}}
{"user": "John", "source": "ESPN", "headline": "Sports news", "timestamp": "2015-07-28T00:09:32.000"}
{"index":{"_index":"news","_type":"news"}}
{"user": "John", "source": "ESPN", "headline": "More sports news", "timestamp": "2015-07-28T00:10:35.000"}
{"index":{"_index":"news","_type":"news"}}
{"user": "Mary", "source": "Yahoo", "headline": "More news", "timestamp": "2015-07-28T00:11:54.000"}
{"index":{"_index":"news","_type":"news"}}
{"user": "Mary", "source": "Yahoo", "headline": "Crazy news", "timestamp": "2015-07-28T00:12:31.000"}
'
So how do I get the last CNN and last ESPN headlines from John for example?
I've been looking into the multi search API, but this would mean that I would need to know all the sources beforehand (in this case CNN and ESPN).
First, please note that I had to change your mapping for the headline field to string, as in your sample documents headlines are strings and not objects.
So, a query like the following one would retrieve what you expect:
curl -XPOST "$ELASTICSEARCH_ENDPOINT/news/_search" -d '{
"size": 0,
"query": {
"filtered": {
"filter": {
"term": {
"user": "John" <--- filter for user=John
}
}
}
},
"aggs": {
"sources": {
"terms": {
"field": "source" <--- aggregate by source
},
"aggs": {
"latest": {
"top_hits": {
"size": 1, <--- only take the first...
"_source": [ <--- only the date and headline
"headline",
"timestamp"
],
"sort": {
"timestamp": "desc" <--- ...and only the latest hit
}
}
}
}
}
}
}'
That will yield something like this:
{
...
"aggregations" : {
"sources" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "CNN",
"doc_count" : 2,
"latest" : {
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "news",
"_type" : "news",
"_id" : "AU7Sh3VDGDddn2ZNuDVl",
"_score" : null,
"_source":{
"headline": "More great news",
"timestamp": "2015-07-28T00:08:23.000"
},
"sort" : [ 1438042103000 ]
} ]
}
}
}, {
"key" : "ESPN",
"doc_count" : 2,
"latest" : {
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "news",
"_type" : "news",
"_id" : "AU7Sh3VDGDddn2ZNuDVn",
"_score" : null,
"_source":{
"headline": "More sports news",
"timestamp": "2015-07-28T00:10:35.000"
},
"sort" : [ 1438042235000 ]
} ]
}
}
} ]
}
}
}