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