2017-07-27 466 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」',但是能夠以多種不同方式分析和索引字符串的原理仍然存在。人們仍然將此稱爲多字段或子字段(是否有更好的名稱?):) –