我遇到了弹性搜索问题。 我希望能够对结果进行排序,但elasticsearch忽略了排序标记。我的查询在这里:
{
"sort": [{
"title": {"order": "desc"}
}],
"query":{
"term": { "title": "pagos" }
}
}
然而,当我删除查询部分并且我只发送sort标签时,它可以工作。 有人能指出我正确的方法吗?
我也尝试过以下查询,这是我的完整查询:
{
"sort": [{
"title": {"order": "asc"}
}],
"query":{
"bool":{
"should":[
{
"match":{
"title":{
"query":"Pagos",
"boost":9
}
}
},
{
"match":{
"description":{
"query":"Pagos",
"boost":5
}
}
},
{
"match":{
"keywords":{
"query":"Pagos",
"boost":3
}
}
},
{
"match":{
"owner":{
"query":"Pagos",
"boost":2
}
}
}
]
}
}
}
设置
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "ngram",
"min_gram": 3,
"max_gram": 15,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"default" : {
"tokenizer" : "standard",
"filter" : ["standard", "lowercase", "asciifolding"]
},
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding",
"autocomplete_filter"
]
}
}
}
}
}
映射
{
"objects": {
"properties": {
"id": { "type": "string", "index": "not_analyzed" },
"type": { "type": "string" },
"title": { "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard" },
"owner": { "type": "string", "boost": 2 },
"description": { "type": "string", "boost": 4 },
"keywords": { "type": "string", "boost": 1 }
}
}
}
提前致谢!
答案 0 :(得分:20)
文档中的字段"标题" 是 已分析的 字符串字段,该字段也是一个多值字段,这意味着elasticsearch会将字段的内容拆分为标记,并将其单独存储在索引中。 您可能希望在第一个字词中按字母顺序对"标题" 字段进行排序,然后在第二个字词中对其进行排序,依此类推,但是elasticsearch没有此信息可用于排序时间。
因此,您可以更改"标题" 字段的映射:
{
"title": {
"type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard"
}
}
进入这样的多字段映射:
{
"title": {
"type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer":"standard",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
现在根据 分析 "标题" 字段执行搜索,并根据 not_analyzed " title.raw" 字段
{
"sort": [{
"title.raw": {"order": "desc"}
}],
"query":{
"term": { "title": "pagos" }
}
}
这里有很好的解释:String Sorting and Multifields