2015-12-28 95 views
11

我遇到了elasticsearch查询的问题。 我希望能够排序结果,但elasticsearch忽略排序标记。在这里我的查询:Elasticsearch没有对结果进行排序

{ 
    "sort": [{ 
     "title": {"order": "desc"} 
    }], 
    "query":{ 
     "term": { "title": "pagos" } 
    } 
} 

然而,当我删除查询的一部分,我只发送排序的标签,它的工作原理。 任何人都可以指出我的正确方法吗?

我也试着用下面的查询,这是我有完整的查询:

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

什么结果你得到和你有什么期望?还请说明你如何发送你的查询(curl,Java,Python,Sense等)? – Val

+0

谢谢,我正在使用python(http://elasticsearch-dsl.readthedocs.org/en/latest/)。问题是elasticsearch总是返回相同的结果。查询部分工作正常,但返回相同的列表为asc和desc命令。 –

+0

你能显示你正在使用的Python代码吗? – Val

回答

14

字段“标题”文档中为分析字符串字段,这也是一个mutivalued字段,这意味着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" 
     } 
    } 
    } 
} 

现在执行上基于搜索“title”字段和排序基于not_analyzed“title.raw”

{ 
    "sort": [{ 
     "title.raw": {"order": "desc"} 
    }], 
    "query":{ 
     "term": { "title": "pagos" } 
    } 
} 

它是美丽的这里解释:String Sorting and Multifields

+0

谢谢。这正是问题所在。现在正在努力。你救了我的一天! –

相关问题