2017-07-27 468 views
0

我正在尝试搜索并对结果进行排序。但是,我收到一个错误,不知道为什么。ElasticSearch - 排序不起作用

编辑 - 我会提供我的完整映射。

"myindex": { 
     "mappings": { 
     "mytype": { 
      "dynamic_templates": [ 
       { 
        // Dynamic templates here! 
       } 
      ], 
      "properties": { 
       "fieldid": { 
        "type": "keyword", 
        "store": true 
       }, 
       "fields": { 
        "properties": { 
        "myfield": { 
         "type": "text", 
         "fields": { 
          "sort": { 
           "type": "keyword", 
           "ignore_above": 256 
          } 
         }, 
         "analyzer": "myanalyzer" 
        }      
        } 
       }, 
       "isDirty": { 
        "type": "boolean" 
       } 
      } 
     } 
     } 
    } 
} 

当我执行搜索与整理,就像这样:

POST /index/_search 
{ 

    "sort": [ 
    { "myfield.sort" : {"order" : "asc"}} 
    ] 
} 

我得到以下错误:

{ 
    "error": { 
     "root_cause": [ 
     { 
      "type": "query_shard_exception", 
      "reason": "No mapping found for [myfield.sort] in order to sort on", 
      "index_uuid": "VxyKnppiRJCrrnXfaGAEfA", 
      "index": "index" 
     } 
     ] 
    "status": 400 
} 

我在下面的文档elasticsearch。 DOCUMENTATION

我也检查此链接: DOCUMENTATION

有人能为我提供帮助?

+0

你明白了什么叫当'卷曲-XGET本地主机:9200/index'? – Val

+0

我得到映射。但问题不在于映射,而在于搜索。您可以在最后的评论中查看解决方案。无论如何,谢谢:) – NatsuDragonEye

回答

1

嗯,这可能是你的映射设置不正确。我跟着一起使用下列内容:

PUT /your-index/ 
{ 
    "settings": { 
     "number_of_replicas": "1", 
     "number_of_shards": "3", 

     "analysis": { 
      "customanalyzer": { 
       "ID": { 
        "type": "custom", 
        "tokenizer": "keyword", 
        "filter": ["lowercase"] 
       } 
      } 
     } 
    }, 
    "mappings": { 
     "thingy": { 
      "properties": { 
       "myfield": { 
        "type": "text", 
        "analyzer": "ID", 

        "fields": { 
         "sort": { 
          "type": "keyword", 
          "ignore_above": 256 
         } 
        } 
       } 
      } 
     } 
    } 
} 

要仔细检查,如果该指数实际上有myfield.sort场看看

GET /your-index/thingy/_mapping 

然后,上传一些文件(不需要指定的子场(S) ,elasticsearch会为你做它)

POST /your-index/thingy/ 
{ 
    "myfield": "some-value" 
} 

现在我可以用下面的搜索:

POST /your-index/thingy/_search 
{ 
    "sort": [ 
     { "myfield.sort": { "order": "asc" } } 
    ] 
} 

所以一定要检查:

  • 命名/错字的(你永远不知道)
  • 你映射(它有“myfield.sort”字段)
  • 你是否在正确的搜索指数?

希望这有助于

+0

我会把它称为一个'multi_field'或子字段,而不是_nested field_,因为这意味着不同的东西:) –

+0

好点,我改变了一下,但multi_fields似乎是... https://www.elastic.co/guide/en/elasticsearch/reference/1.4/_multi_fields.html不见了? :D – Tessmore

+0

'multi_field' _types_消失了,即不再需要指定'“type”:“multi_field”',但是能够以多种不同方式分析和索引字符串的原理仍然存在。人们仍然将此称为多字段或子字段(是否有更好的名称?):) –