2016-03-31 13 views
0

ben kütüphaneyi kullanıyorum elasticsearchgo + panik elastigo: çalışma zamanı hatası: endeksi aralıkta

ile golang test ediyorum dışına i çalıştırdığınızda https://github.com/mattbaird/elastigo

Benim sorundur:

go run elastigo_postal_code2.go 
böyle

derleyici gösterisi şey:

panic: runtime error: index out of range 

goroutine 1 [running]: 
panic(0x893ce0, 0xc82000a150) 
     /opt/go/src/runtime/panic.go:464 +0x3ff 
main.main() 
     /home/hector/go/elastigo_postal_code2.go:80 +0xa30 
exit status 2 

değilim Bunun ne anlama geldiğini veya nasıl düzeltebileceğimi bilmelisiniz!

Birisi Scritp kodu burada

/* 

curl -X PUT "http://localhost:9200/mx2/postal_code/1" -d " 
{ 
    \"cp\"   : \"20000\", 
    \"colonia\" : \"Zona Centro\", 
    \"ciudad\"  : \"Aguascalientes\", 
    \"delegacion\" : \"Aguascalientes\", 
    \"location\": { 
     \"lat\": \"22.0074\", 
     \"lon\": \"-102.2837\" 
    } 
}" 

curl -X PUT "http://localhost:9200/mx2/postal_code/2" -d " 
{ 
    \"cp\"   : \"20008\", 
    \"colonia\" : \"Delegacion de La Secretaria de Comercio y Fomento Industrial\", 
    \"ciudad\"  : \"Aguascalientes\", 
    \"delegacion\" : \"Aguascalientes\", 
    \"location\": { 
     \"lat\": \"22.0074\", 
     \"lon\": \"-102.2837\" 
    } 
}" 

*/ 

package main 

import (
    "encoding/json" 
    "flag" 
    "log" 
    elastigo "github.com/mattbaird/elastigo/lib" 
) 

var (
    eshost *string = flag.String("host", "localhost", "Elasticsearch Server Host Address") 
) 


func main() { 
    flag.Parse() 
    log.SetFlags(log.Ltime | log.Lshortfile) 

    c := elastigo.NewConn() 
    c.Domain = *eshost 

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    searchJson := `{ 
     "size": 10, 
     "query": { 
      "match": { 
       "all": { 
        "query": "aguascalientes", 
        "operator": "and" 
       } 
      } 
     }, 
     "sort": [{ 
      "colonia": { 
       "order": "asc", 
       "mode": "avg" 
      } 
     }] 
    }` 

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    searchresponse, err := c.Search("mx2", "postal_code", nil, searchJson) 
    if err != nil { 
     log.Println("error during search:" + err.Error()) 
     log.Fatal(err) 
    } 

    // try marshalling to ElasticSearchResponse type 
    var t ElasticSearchResponse 
    bytes, err := searchresponse.Hits.Hits[0].Source.MarshalJSON() 
    if err != nil { 
     log.Fatalf("err calling marshalJson:%v", err) 
    } 

    json.Unmarshal(bytes, &t) 
    log.Printf("Search Found: %s", t) 
    c.Flush() 

} 

func (t *ElasticSearchResponse) String() string { 
    b, _ := json.Marshal(t) 
    return string(b) 
} 

// used in test suite, chosen to be similar to the documentation 
type ElasticSearchResponse struct { 
    Cp   string `json:"cp"` 
    Colonia string `json:"colonia"` 
    Ciudad  string `json:"ciudad"` 
    Delegacion string `json:"delegacion"` 
    Location Location `json:"location"` 
} 

type Location struct { 
    Lat string `json:"lat"` 
    Lon string `json:"lon"` 
} 

yanlıştır yapıyorum bana yardımcı ve bana söyleyebilir:

bir derleme hatası değil

https://gist.github.com/hectorgool/c9e18d7d6324a9ed1a2df92ddcc95c08#file-elastigo_example-go-L80

cevap

0

, sen panic: runtime error: index out of range alıyorsanız

Dilim searchresponse.Hits.Hits öğesinin uzunluğu 0 ise, ilk öğenin indekslenmesi aralık dışı olur ve panik. Hits sayısını önce searchresponse.Hits.Len() ile kontrol edebilirsiniz.

İlgili konular