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

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 }

Related

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

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,
});

multiple select in graphql query

I am started learn graphql and use SpaceX api (https://api.spacex.land/graphql/)
I have theese query:
{
shipsResult(
limit: 5,
offset: 0,
find: {
type: "Cargo",
home_port: "Port Canaveral"
}) {
result {
totalCount
}
data {
id
name
type
home_port
}
}
}
I need select more than one value in home_port parameter, like array ["Port Canaveral", "Port of Los Angeles"]
What i shall do?

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"
}
}
}
});```

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' } }

How do I use More Like This API in Elasticsearch for ruby

I couldn't find any documentation about this at all. Or, maybe I'm missing something. I'm trying to use Elasticsearch's More Like This API with the elasticsearch gem.
I've indexed all documents already.
Here's my code:
require 'elasticsearch'
client = Elasticsearch::Client.new log: true
client.search index: 'movies', body: { query: { match: { description: 'test' } } }
client.search index: 'movies', body: { more_like_this: {
fields: ['description'],
like_text: 'Once upon a time'
} }
I'm getting this error:
Parse Failure [No parser for element [more_like_this]]];
}{[2fk_R7WbTky6UfMCbKPKGA][movies][1]
Try wrapping your more_like_this in query, like so:
client.search index: 'movies', body: {
query: {
more_like_this: {
fields: ['description'],
like_text: 'Once upon a time'
}
}
}
Hope this helps!

Resources