Counting documents by property occurrence in Kibana - elasticsearch

I'm trying to create a visualization that looks like this:
Foobar, 10
Bar, 8
Baz, 5.6
The first column is the aggregation itself. Imagine i have documents like this:
{
id: 1,
name: 'lorem ipsum',
type: 'A'
author: {
name: 'Foobar',
}
}
{
id: 2,
name: 'dolor sit amet',
type: 'B',
author: {
name: 'Foobar',
}
}
So, i want to add a +1 to the score of "Foobar" everytime i find a document of type A. And a +2 to the score if i find a document of type B. Basically, aggregating by the author name, and calculating a dynamic value on results.
Is this possible in Kibana? Thanks for the help.

AFAIK, you can't do this in Kibana in visualize panel, maybe you can try it in program then index the result into es.

Related

Is there a way to create a runtime field in Elasticsearch that is equal to a 'Value'/'Sum of Value across index'?

I have a task to show the percent of value a set of filtered documents represents vs the entire value represented across a whole year. For example:
[{
name: 'Foo',
value: 12,
year: 2021
},
{
name: 'Bar',
value: 2,
year: 2021
},
{
name: 'Car',
value: 10,
year: 2021
},
{
name: 'Lar',
value: 4,
year: 2022
}]
I'd like to create a runtime field that would equal .5 for 'Foo' (12/(12+2+10)), .42 for 'Car' (10/(12+2+10)) and 1 for 'Lar' (4/4). Is this possible? Is there a better way to achieve this result? The ultimate goal is that if someone creates a query that returns 'Foo' and 'Car' they could sum the runtime field to get .92 (.5+.42) and that such a result could be used in a Kibana Lens visualization.
I've tried creating queries that return the above results, and that is easy enough, but those queries aren't usable inside Kibana which also has global filters to account for. That's why I thought a calculated field that represents the ratio of a document's value in relation to the sum of all documents' values would be useful.

Adding fuzziness to an ElasticSearch prefix query

I have two documents:
{id: 1, name: "james"}
{id: 2, name: "james kennedy"}
I am using the match_bool_prefix API for autocomplete, and I would like to be able to match the document with id: 1 even if I incorrectly spell james.
Query: jamis
Desired output: finding document with id: 1.

elasticsearch return unique matched values

I recently started looking at elasticsearch, I'm in the process of learning what it can do and decide how I can use it in my projects.
For one project I used a couchdb (noSQL) database. The client can search it using couchdb views. Easy, but limited in functionality.
I'd like to have elasticsearch to open up the data in a far more rich way.
Searching for composers and titles of musical pieces is now handled by elasticsearch with amazingly fast 'query_string's. And it's fuzzy!
There is one thing however I did not manage to accomplish with elasticsearch, but I'm pretty sure it's possible, I'm just missing it.
It's about the autocomplete functionality when entering instrument names.
For example:
I have 2 documents (musical pieces) with different instruments needed to play them:
{
title: 'Awesome Piece',
authors: [{
name: 'John Doe',
role: 'composer'
}, {
name: 'Shakespeare',
role: 'lyricist'
}],
instruments: [
'soprano',
'alto',
'tenor',
'bass',
'trumpet',
'trumpet',
'piano'
]
}
{
title: 'Not so Awesome Piece',
authors: [{
name: 'Another J. Doe',
role: 'composer'
}, {
name: 'Little John',
role: 'arranger'
}],
instruments: [
'trombone',
'organ'
]
}
To enter a new musical piece there is a field to insert instrument names. I'd like the offer an autocomplete.
So if the user types 't', I want a list of all instruments matching 't*': ['tenor', 'trumpet', 'trombone'], if he types 'tr', I need: ['trumpet', 'trombone']
The best I coold find was a query with an aggregation, but it searches for documents and aggregates them as a whole, returning all instruments of the document(s) found with the query.
And off course, I want the autocomplete to be fuzzy in the end.
Can anybody point me in a direction?
Thanks in advance!
(I'm running elasticsearch 2.3, but I don't mind upgrading!)

Elasticsearch Aggregation Huge Bucket

I have an category tree on each document. Like
[{
name: "RootCat",
parentId: 104319,
id: 104319
},
{
name: "FirstLevel",
parentId: 104319,
id: 104328
},
{
name: "n Level",
parentId: 104328,
id: 107929
}]
The problem when i want to have a tree for each search like :
Root Cat
- First Level
--Second Level-1
--Second Level-2
Aggregations come up with all buckets. And i have about 40000 categories, so it makes huge network traffic. How can i get only categories that i want to show.My filter aggreagtion below
.Filter(SearchConstants.Aggregation.Category,
y => y.Aggregations(r => r.Filter("filteredAggs",
cc => cc.Filter(GetPostFilters(searchQuery)) .Aggregations(ra => ra.Nested("cat",
ty => ty.Path(rtw => rtw.CategoryList).Aggregations(
abc => abc.Terms("categoryId", t => t.Field(q => q.CategoryList.First().Id).Size(0)
I want to get one level children with all parents. Like
Root Cat
Firts Level
Second Level
(But buckets have all level children)
Not sure what you mean, by default the search results impact the actual aggregations that are returned. Another option is to use a filter in the aggregation itself. But this is dependent on your requirements. Check the docs for implementing a filter in the aggregation:
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-filter-aggregation.html

Complex CouchDb View

I have the following documents in a database:
{
id: 1,
text: 'Hello I had a big grannysmith apple today and a big pear'
},
{
id: 2,
text: 'Hello I had a big apple today only'
},
{
id: 3,
text: 'Hello I had a big apple today, a big pear yesterday and a big orange today'
}
My view needs to return an aggregated count of specific keywords in the text, but the keywords need to be 'loose' on how they found. My view should return something like this:
{
'grannysmith apple' : 3,
'pear': 2,
'orange': 1
}
As you can see I have counted apples 3 times even though the tag is for 'grannysmith apple', I still want to pick up any occurrences of 'apples' as well.
Is this possible? Or should I be doing this before I insert into CouchDb? I'm using node.js to perform the saving - should I do it in node.js?
Thanks in advance!
I think you should use RegExp objects as keywords and aggregate with group_level.

Resources