Bug: Mutation issue with facets integer

Moved from GitHub dgo/3

Posted by tyuwan:

I apologize, if this is not the correct place to post this. When I set facets with int(1) value it stored as float (1.000000). It’s perfectly fine if I post or query with HTTP

type GraphCollection struct {
	Uid           string      `json:"uid"`
	Weight        []GraphNode `json:"weight,omitempty"`
}

type GraphNode struct {
	Uid          string `json:"uid"`
	WeightFacets int64  `json:"weight|score,omitempty"`
}

data := GraphCollection{
		Uid: cuid,
		Weight: []GraphNode{
			{
				Uid:          muid,
				WeightFacets: 1,
			},
		},
	}

	ctx := context.Background()

	dg, conn := dgraph.NewClient()
	defer conn.Close()

	pb, err := json.Marshal(data)
	if err != nil {
		log.Fatal(err)
	}

	_, err = dg.NewTxn().Mutate(ctx, &api.Mutation{SetJson: pb, CommitNow: true})
	if err != nil {
		log.Error(err)
	}

marshal string:
{"uid":"0x9c6711","weight":[{"uid":"0x1053b2","weight|score":1}]}

	q := fmt.Sprintf(`{
	  me(func: uid(0x9c6711)) {
	    weight @facets {
	      uid
	    }
	  }
	}`)

	txn := dg.NewTxn()
	resp, err := txn.Query(ctx, q)

	if err != nil {
		log.Fatal(err)
	}

	log.Debug(string(resp.Json))

response:
{"me":[{"weight":[{"uid":"0x1053b2","weight|score":1.000000}]}]}
with error
json: cannot unmarshal number 1.000000 into Go struct field weight of type int

pawanrawal commented :

Since the mutation input is in JSON format which doesn’t differentiate between INT/FLOAT (everything is a number), the server gets a float and stores the facet as such. This is, unfortunately, a limitation of the JSON format and the fact that facets don’t have a fixed type.

I would recommend setting a property on a node (instead of facet) if the type is important to you or using the RDF mutation format.