Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSearch fail to work with raw json #705

Closed
kartlee opened this issue Feb 9, 2018 · 5 comments
Closed

MultiSearch fail to work with raw json #705

kartlee opened this issue Feb 9, 2018 · 5 comments

Comments

@kartlee
Copy link

kartlee commented Feb 9, 2018

Please use the following questions as a guideline to help me answer
your issue/question without further inquiry. Thank you.

Which version of Elastic are you using?

[ ] elastic.v2 (for Elasticsearch 1.x)
[ ] elastic.v3 (for Elasticsearch 2.x)
[ * ] elastic.v5 (for Elasticsearch 5.x)
[ ] elastic.v6 (for Elasticsearch 6.x)

Please describe the expected behavior

Does multisearch accept raw json for each elastic search request?

Please describe the actual behavior

I am using multisearch request containing each elastic search request with raw json and index to operate on. I am getting error from elastic search as - "elastic: Error 400 (Bad Request): Expcted [START_OBJECT] but found [VALUE_STRING] [type=parsing_exeception".

Any steps to reproduce the behavior?

@olivere
Copy link
Owner

olivere commented Feb 9, 2018

Can you send a working sample of your code so that I can reproduce?

@kartlee
Copy link
Author

kartlee commented Feb 9, 2018

I just took the exisiting msearch_test.go and modified to reproduce the issue. Please find below. I am not sure if I am passing raw json with right API. Let me know.

func TestMultiSearch(t *testing.T) {
        client := setupTestClientAndCreateIndex(t)

        tweet1 := tweet{
                User:    "olivere",
                Message: "Welcome to Golang and Elasticsearch.",
                Tags:    []string{"golang", "elasticsearch"},
        }
       tweet2 := tweet{
                User:    "olivere",
                Message: "Another unrelated topic.",
                Tags:    []string{"golang"},
        }
        tweet3 := tweet{
                User:    "sandrae",
                Message: "Cycling is fun.",
                Tags:    []string{"sports", "cycling"},
        }

        // Add all documents
        _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
        if err != nil {
                t.Fatal(err)
        }

        _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
        if err != nil {
                t.Fatal(err)
        }

        _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
        if err != nil {
                t.Fatal(err)
        }

        _, err = client.Flush().Index(testIndexName).Do(context.TODO())
        if err != nil {
                t.Fatal(err)
        }

        /*
        // Spawn two search queries with one roundtrip
        q1 := NewMatchAllQuery()
        q2 := NewTermQuery("tags", "golang")

        sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).
                Source(NewSearchSource().Query(q1).Size(10))
        sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet").
                Source(NewSearchSource().Query(q2))
        */
       sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2).Source(`
            {
                "query": {
                    "match_all": {}
                }
            }
        `)
        sreq2 := NewSearchRequest().Index(testIndexName).Source(`
            {
                "query": {
                    "term": {
                        "tags": "golang"
                   }
                }
            }
        `)

        searchResult, err := client.MultiSearch().
                Add(sreq1, sreq2).
                Do(context.TODO())
        if err != nil {
                t.Fatal(err)
        }
        if searchResult.Responses == nil {
                t.Fatal("expected responses != nil; got nil")
        }
        if len(searchResult.Responses) != 2 {
                t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses))
        }

        sres := searchResult.Responses[0]
        if sres.Hits == nil {
                t.Errorf("expected Hits != nil; got nil")
        }
        if sres.Hits.TotalHits != 3 {
                t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits)
        }
        if len(sres.Hits.Hits) != 3 {
                t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits))
        }
        for _, hit := range sres.Hits.Hits {
                if hit.Index != testIndexName {
                        t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
                }
                item := make(map[string]interface{})
                err := json.Unmarshal(*hit.Source, &item)
                if err != nil {
                        t.Fatal(err)
                }
        }

        sres = searchResult.Responses[1]
        if sres.Hits == nil {
                t.Errorf("expected Hits != nil; got nil")
        }
        if sres.Hits.TotalHits != 2 {
                t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits)
        }
        if len(sres.Hits.Hits) != 2 {
                t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits))
        }
        for _, hit := range sres.Hits.Hits {
                if hit.Index != testIndexName {
                        t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
                }
                item := make(map[string]interface{})
                err := json.Unmarshal(*hit.Source, &item)
                if err != nil {
                        t.Fatal(err)
                }
        }
}

@kartlee
Copy link
Author

kartlee commented Feb 10, 2018

The problem might be here -

body, err := json.Marshal(sr.Body())
when string json is provided. On the other hand, elastic search body for msearch should not be pretty printed. I should avoid it.

olivere added a commit that referenced this issue Feb 10, 2018
@olivere
Copy link
Owner

olivere commented Feb 10, 2018

Thanks. I could reproduce the issue and there's a fix in the next release. I hope to get it out this weekend.

@olivere olivere closed this as completed Feb 10, 2018
@kartlee
Copy link
Author

kartlee commented Feb 12, 2018

Super, thanks for fixing it quickly.

-Karthik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants