The Elasticsearch sort uses-es common sorting methods __elasticsearch

Source: Internet
Author: User

In the search system, one of the most common needs is to sort, and also the search engine needs to change the most places.


Common sorts generally have the following two categories:
(1) According to similarity degree matching score ranking

(2) Sorting similarity score according to the specified field is the system default sort, and more often, we need to sort by the specified fields. In Lucene, the sort based on the field content is implemented by the Fieldcomparator interface, which is divided into the sorting of single and multiple fields. In terms of sorting speed, numeric types are sorted fastest, and in Lucene the sort of numeric types is implemented by Numericcomparator derived classes that support float, double, int, long, and numericcomparator derived classes. Four-bell types (dates are converted to long storage at the bottom).
A field with a string type generally needs to be searched, so it needs to be treated with word segmentation. The result of participle processing is to parse the string into multiple entries. At the Lucene level, you can get multiple index entries based on DocId, which is correct regardless of which index entry is used.
So in order to sort the string type field, you need to save the entire string as an index entry and, in order to ensure that the field can be searched, also save the result of the string analyzed (that is, you need to keep the complete string as an entry, while the word is indexed).

In the ES for the realization of this function, ES provides a multi-field mapping mechanism, configuration can be configured to achieve the field that is participle, but also can be sorted:

"title": {
' Type ': ' String ',
"Analyzer": "ANSJ",
"Fields": {
"Sort": {
' Type ': ' String ',
"Index": "Not_analyzed"
}
}
}

Use the Title field as a search, and the Title.sort field as a sort:

Get/_search
{
"Query": {
"Match": {
"title": "Elasticsearch"
}
},
"Sort": "Title.sort"

}


Multi-field ordering in Lucene standard syntax:
{
"From": 0,
"Size": 10,
"Query": {
"Query_string": {"Query": "*:*", "Default_operator": "and"}
},
"Sort": [

{

' Title.sort ': {' order ': ' desc '}, ' _score ': {' order ': ' Desc '}
}
]

}

Default Field Sort:

{
"From": 0,
"Size": 10,
"Query": {
"Query_string": {"Query": "Title:test", "Default_operator": "and"}
},
"Sort": [
{
"Rpgendatetime": {"order": "desc", "Missing": "_last"}
}
]
}

To sort by using a script:

{"
    query": {"
     query_string": {   "query": "",   "Default_operator": "and"      }
    , "sort": {"_script": {"
            Type": "Number",
            "script": {
                "inline": "doc[' Field_name '"]. Value * factor ",
                " params ": {
                    " factor ": 1.1
                }
            },
            " Order ":" ASC "
        }
}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.