prefix query, returns results with constant score. How to sort results alphabetically by a field in ascending in JAVA?
I would have a non-analyzed multi-field [1] and then sort [2] on that field.
{
"mappings": {
"my_type": {
"properties": {
"my_field": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
{
"sort": [
{
"my_field.raw": {
"order": "asc"
}
}
],
"query": {
...
}
}
[1]
https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html
[2]
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
Thanks much for reply. I am using elasticsearch Java API. Document has multiple fields like symbol, name and description. Document should be picked if search term matches any of these 3 fields, even partial match like prefix/match. eg: if search term is "Bp" some of the documents picked by symbol partial matches like "Bp123", "Bp1"," Bp150". They all have same _score. How to sort them? I am using a compound query DismaxQueryBuilder to build query adding queries match by NAME, Match By Symbol, Match by DESCRIPTION. Relevance returned by Elasticsearch by _score is good. Only when scores are same, it would be good to have results in sorted order. How can I achieve this in java api.
so as per reply, if we add sort to raw field in mappings at index time, will search results be returned in that order if we query that raw field.
I don't want to change the relevance returned based on scores, but for one of the queries like prefix in the dismaxquerybuilder the _score are constant, so I want to sort only those results using any of the fields. eg: like "Bp123", "Bp1"," Bp150" with prefix query the scores are constant. I want the results like Bp1, Bp123, Bp150 . If I add SORT by field in the search request builder where the query set is dismaxquery of mutiple fields, all the documents are sorted by that field and relevance is gone. I want to sort those documents only whose scores are equal not all the documents.
You can define a secondary sort if the first sort returns exact matches.
{
"sort": [
{
"_score",
"my_field.raw": {
"order": "asc"
}
}
],
"query": {
...
}
}
You do not query the raw field, only use it to sort by. The above sort will
still score the documents and order them by score (descending), but you
order items by the non analyzed field name if the score is identical.
Thanks Ivan. I am new to elasticsearch. I am using DismaxQueryBuilder to get union of all the documents matched by matchQuery by SYMBOL, matchQuery by NAME, matchQuery by DESCRIPTION, prefixQuery by SYMBOL, prefixQuery by NAME and prefixQuery by DESCRIPTION. Documents picked by matchQuery has relevance by score, exactly what I wanted. But documents picked by prefix query have no relevance, _scores are same, so I wanted those documents to be sorted by SYMBOL. Eg; the symbols in the documents are BP5, BP1, BP9, when I searched for BP all the three documents are picked by prefix query with no relevance which I wanted to be sorted like BP1, BP5, BP9 plus some other documents by matchQuery by other fields with relevance as expected and need not to be sorted.
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.