Elasticsearch: sort by copy_to target of two fields - sorting

I'm using trying to create a fullName using copy_to exactly like docs say to do: https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html
But, what I want to do in a query, is to sort by fullName. However, when I specify the field to sort I see that the query is actually sorted by forename, e.g. the first part of copy_to:
{
"sort": [
{
"<nested>.fullName.keyword": {
"nested_path": "<nested>",
"order": "desc"
}
}
]
}
What I want to do is to sort by forename + surname e.g. by full fullName.
Is it possible to do this using copy_to at all?

Related

Custom sort lexicographically as int

I have some elastic elements that have a string property that looks like 10/2021 and it need to be sorted as a int, but when I perform this query
"sort": [
{
"myProperty": {
"order": "asc"
}
},
I get the lexicographic order.
1/2021
10/2021
100/2021
101/2021
102/2021
But I need it to sort by the first number and the year like this:
1/2020
2/2020
...
1/2021
2/2021
I can't figure out how to custom sort, is it even possible?
Solution 1:
Using Scripted-Sort ...
Not Recommended with large data-set: It will take time as we are performing computations here
GET <>/_search
{
"query": {
"match_all": {}
},
"sort": {
"_script":{
"type":"number",
"script":{
"lang":"painless",
"source":"Integer.parseInt(doc['myProperty.keyword'].value.replace(\"/\",\"\"))" //<====== Replace myProperty.keyword with the keyword field or String field with field-data true
}
}
}
}
Note: i haven't added null checks in the script, just in case you have any document which don't have this field.
Solution 2:
Store another Numeric field in elastic search which doesn't have "/"
Sort based on that field
Migrate the data of existing documents to the field using update_by_query API
This is the Recommended approach.

Multi_match elasticsearch on all fields with boost to specific fields

I am using Elastic 6.1+
I have created an index and added some values to it, the index mapping is text and numbers.
I want to create a multi_match on all of the fields in the index, query a text or a number and get the results back.
Also i would like to define that the score of field1 on the index is boosted
For some reason once i add the fields array it only search on that fields (added it in order to be able to define which field i want to boost and how much) and if i add to the fields array the "*" as field it return an error.
GET MyIndex/_search
{
"query": {
"multi_match": {
"query": "test1",
"fields": [
"field1^3",
"*"
]
}
}
}
Thank you
Apparently adding
"lenient": true
to the query solved the problem

How can you sort by the lesser of two dates in Elasticsearch?

Scenario:
I have data that has two date fields (created_at and published_on). In some cases the dates are not right, and I need to sort by the lesser (older) of the two dates.
In other words, for each item, figure out the older date, and then use that to sort on.
I see from the documentation that you can sort by fields "sequentially" (eg: sort by name and then sort by city), and I see some numerical sort options (min/max, etc). But I'm struggling with how to achieve this.
This post describes how this works in something like PostgreSQL but I know that Elastic is a completely different animal, so I'm hoping there's some way to do this.
Thanks!
You need to extract one value out of the two fields and then sort based on that value. For this you need script based sorting. In your query dsl you have to add the sort param as below:
{
"sort": {
"_script": {
"type": "number",
"script": {
"source": "if(doc['created_at'].value.getMillis() < doc['published_on'].value.getMillis()) { return doc['created_at'].value.getMillis(); } else { return doc['published_on'].value.getMillis(); }",
"lang": "painless"
},
"order": "desc"
}
}
}

Elasticsearch order by type

I'm searching an index with multiple types by simply using 'http://es:9200/products/_search?q=sony'. This will return a lot of hits with many different types. The hits array contains all the results but not in the order I want it to; i want the 'television' type to always show before the rest. Is it possible at all to order by type?
You can achieve this by sorting on the pre-defined field _type. The query below sorts results in ascending order of document types.
POST <indexname>/_search
{
"sort": [
{
"_type": {
"order": "asc"
}
}
],
"query": {
<query goes here>
}
}
I do it by adding a numeric field _is_OF_TYPE to the indexed documents and set it to 1 for those docs that are of the given type. Then just sort on those fields in any order you want.
For example:
Document A:
{
_is_television: 1,
... some television props here ...
}
Document B:
{
_is_television: 1,
... another television props here ...
}
Document C:
{
_is_radio: 1,
... some radio props here ...
}
and so on...
Then in ElasricSearch query:
POST radio,television,foo,bar,baz/_search
{
"sort": [
{"_is_television": {"unmapped_type" : "long"}}, // television goes first
{"_is_radio": {"unmapped_type" : "long"}}, // then radio
{"_is_another_type": {"unmapped_type" : "long"}} // ... and so on
]
}
The benefit of this solution is speed. You simply sort on numeric fields. No script sorting required.

Sorting a match query with ElasticSearch

I'm trying to use ElasticSearch to find all records containing a particular string. I'm using a match query for this, and it's working fine.
Now, I'm trying to sort the results based on a particular field. When I try this, I get some very unexpected output, and none of the records even contain my initial search query.
My request is structured as follows:
{
"query":
{
"match": {"_all": "some_search_string"}
},
"sort": [
{
"some_field": {
"order": "asc"
}
}
] }
Am I doing something wrong here?
In order to sort on a string field, your mapping must contain a non-analyzed version of this field. Here's a simple blog post I found that describes how you can do this using the multi_field mapping type.

Resources