2016-03-31 4 views
0

のうち指数私は、ライブラリを使用していelasticsearch行く+はパニックをelastigo範囲

でgolangをテストしている:私は、実行時に https://github.com/mattbaird/elastigo

は私の問題は、次のとおりです。

go run elastigo_postal_code2.go 

コンパイラショーこのような何か:

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 

私はありませんこれが何を意味するのか、どうやったらそれを修正できるのか

誰かが私のscritpのコードはここに

/* 

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"` 
} 

間違っている私がやっている私を助け、私を伝えることができます:

コンパイルエラーではありません

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

答えて

0

、あなたはpanic: runtime error: index out of rangeを得ています

スライスsearchresponse.Hits.Hitsの長さが0の場合、最初の要素のインデックスは範囲外になります。パニック最初のヒット数はsearchresponse.Hits.Len()で確認できます。