How to GET a document from an index inside of an alias - elasticsearch

I have inserted a few of my indexes inside of an alias because I've updated the index mapping a few times. I am looking to query my indexes behind my alias for potential documents depending on the search criteria.
I've been querying using just indexes up until now, like so -
const { body: clientBody } = await client.search({
index: 'my-index',
filter_path: 'hits.hits._source',
body: { query: { bool: esObject } },
size: 10000,
});
but as I now have multiple indexes I wish to query inside of my alias I have created (my-alias) this no longer works as needed. I've tried replacing the index for alias but this does not work. Is there something I'm missing?
How I've added my indexes to my alias.
await client.indices.updateAliases(
{[
{
add: {
index: 'my-index-1',
alias: 'my-alias',
},
},
{
add: {
index: 'my-index-2'
alias: 'my-alias'
},
},
]});

According to the nodejs package. The index will be used as the target, as we can see from here.
So the solution was to just change my index, and stop it pointing at 'my-index' but rather at 'my-alias' instead.
const { body: clientBody } = await client.search({
index: 'my-alias',
filter_path: 'hits.hits._source',
body: { query: { bool: esObject } },
size: 10000,
});

Related

GraphQL Apollo pagination and type policies

I am really struggling with this concept. I hope someone can help me understand it better.
The documentations uses a simple example and it's not 100% clear to me how it works.
I have tried using keyArgs, but they didn't work, so I adopted to use the args parameter in the read and merge functions. First, let me explain my scenario.
I have a couple of search endpoints that use the same parameters:
{
search:
{
searchTerm: "*",
includePartialMatch: true,
page: 1,
itemsToShow: 2,
filters: {},
facets: [],
orderBy: {}
}
}
So I have setup my type policies like this:
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
searchCategories: typePolicy,
searchBrands: typePolicy,
searchPages: typePolicy,
searchProducts: typePolicy,
},
},
},
});
And I was using a generic typePolicy for them all.
At first, I tried this:
const typePolicy = {
keyArgs: [
"search",
[
"identifier",
"searchTerm",
"includePartialMatches",
"filters",
"orderBy",
"facets",
],
],
// Concatenate the incoming list items with
// the existing list items.
merge(existing: any, incoming: any) {
console.log("existing", existing);
console.log("incoming", incoming);
if (!existing?.items) console.log("--------------");
if (!existing?.items) return { ...incoming }; // First request
const items = existing.items.concat(incoming.items);
const item = { ...existing, ...incoming };
item.items = items;
console.log("merged", item);
console.log("--------------");
return item;
},
};
But this does not do what I want.
What I would like, is for apollo to work as it does normally, but when the "page" changes for any field, it appends it instead of caching a new request.
Does anyone know what I am doing wrong or can provide me with a better example that what is on the documentation?

What do I do when my query string in an elasticsearch is too long and the search doesn't work?

I'm querying Elastic in my GraphQL resolver like this below. The peopleArray under the second multi_match is dynamic and is filled with numbers (e.g. [101, 102, 103...]). This is for different users to get their messages. If the user's peopleArray has 165 items, it works fine. However, I have test users who have 1873 items in the peopleArray and the query doesn't work. And I may need to support users with 60,000 items. Basically, I'm creating a messaging system for employees in a company and all the numbers are all the people in their network of message-sending. If this is not a practical way of doing this, any suggestions would be appreciated. But if it is possible, how do I get my results? (Have not tried this in the Kibana UI yet.)
const esClient = new elasticsearch.Client({
host: config.elasticsearchRoot
});
const result = await esClient.search({
index: `${config.index.messageStream}`,
body: {
query: {
bool: {
must: [
{
multi_match: {
query: tenantId,
fields: [
"tenantId",
"discretionary.clientKey.systemTenantId",
"clientKey.systemTenantId",
"author.tenantId"
],
operator: "OR"
}
},
{
multi_match: {
query: peopleArray,
fields: ["author.peopleId", "details.recipient.peopleId"],
operator: "OR"
}
}
]
}
},
from,
size,
docvalue_fields: [
{
field: "sortDate",
format: "use_field_mapping"
}
],
sort: {
sortDate: {
missing: "_last",
unmapped_type: "long",
order: "desc"
}
}
}
});```

Elastic search API Query a field where a value 'is one of'

Trying to make a query to extract information on restriction similar to 'is one of' as a filter in Kibana.
I successfully extract if it's a strict single value but if multiple - not.
Single value:
response = await client.search({
index: index,
body: {
query: {
match: {"organization_classification_id": 2}
},
_source: {}
}
});
Multiple value: But I also need to have several ids:
response = await client.search({
index: index,
body: {
query: {
match: {"organization_classification_id": [2, 4,7]}
},
_source: {}
}
});
I tried matchAll, multi_match but it doesn't work
I have an error:
Error: [parsing_exception] [multi_match] unknown token [START_ARRAY] after [organization_classification_id], with { line=1 & col=59 }

apollo writeQuery does not work

i have a problem with local apollo modification like this:
`updateBoth: (schema, value) =>
client.writeQuery({
query: queryObject,
variables: { id },
data: {
Object: {
...obj,
value,
objectSchema: {
...obj.objectSchema,
structure: schema,
},
},
},
}),`
i am struggling greatly anybo can help me?
it does not update value (this time but generally acts bad in many situations)

update elasticcache record without totally replacing it

I'm using this to add a bunch of records to the elasticcache index:
def self.index_trends(trend_hashes)
formatted_trends = trend_hashes.map do |trend_hash|
{
index: {
_index: 'trends',
_type: 'trend',
_id: trend_hash["id"],
data: trend_hash
}
}
end
elasticsearch.bulk({
body: formatted_trends
})
end
Now I need to update a record (given its id). I want to just add one key-val to the data hash, not replace the whole state. How can I do this using elasticsearch-ruby?
I dont know how to do this with ruby, but in ES there is update API.
Which look like this
POST test/type1/1/_update
{
"script" : "ctx._source.new_field = \"value_of_new_field\""
}
From example with ruby should be
client.update index: 'myindex', type: 'mytype', id: '1',
body: { script: 'ctx._source.tags += tag', params: { tag: 'x' } }

Resources