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

how to use two contextQuery #329

Closed
zhaochenxiao90 opened this issue Jul 25, 2016 · 3 comments
Closed

how to use two contextQuery #329

zhaochenxiao90 opened this issue Jul 25, 2016 · 3 comments
Assignees

Comments

@zhaochenxiao90
Copy link

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.v3 (for Elasticsearch [2.x)]

Please describe the expected behavior

I have a suggester index.it contains two context. it shows :

localhost:9200/suggester/suggest/_mapping -d '{
    "suggest": {
        "properties": {
            "name": {
                "type": "string"
            },
            "suggest": {
                "type": "completion",
                "analyzer": "simple",
                "search_analyzer": "simple",
                "payloads": true,
                "context": {
                    "app": {
                        "type": "category",
                        "default": 1
                    },
                    "add_app": {
                        "type": "category",
                        "default": 2
                    }
                }
            }
        }
    }
}'

while,in my project,I want to suggest it by contextQuery.the code is :

suggest_build:=elastic.NewCompletionSuggester("suggest")
suggest_build.Field("suggest")
suggest_build.Size(20)
suggest_build.ContextQueries(elastic.NewSuggesterCategoryQuery("app","1"))
suggest_build.ContextQueries(elastic.NewSuggesterCategoryQuery("add_app","2"))

but,this code build a json to es is :

"context":[{"app":"1"},{"add_app":2}]

this is wrong,the correct format is:

"context":{"app":"1","add_app":"2"}

this is why?I don't know how to deal with it.

Please describe the actual behavior

Any steps to reproduce the behavior?

@olivere
Copy link
Owner

olivere commented Jul 25, 2016

According to Elasticsearch master branch, you are correct: It's a map (see here). I can't seem to find the toXContent that serializes the "context" in 2.x though.

The code in Elastic that does it wrong is here. I will need to see if this is wrong/deprecated sometimes during 2.x. And if so, I will fix it on the next release.

@olivere olivere self-assigned this Jul 25, 2016
@zhaochenxiao90
Copy link
Author

case 1:
src, err := q.contextQueries[0].Source()
if err != nil {
return nil, err
}
suggester["context"] = src
default:
//ctxq := make([]interface{}, 0)
ctxq := map[string]interface{}{}
for _, query := range q.contextQueries {
src, err := query.Source()
if err != nil {
return nil, err
}
//ctxq = append(ctxq, src)

        for k, v :=range src.(map[string]interface{}) {
            ctxq[k] = v
        }

    }
    suggester["context"] = ctxq
}

this is correct,you can look

olivere added a commit that referenced this issue Jul 27, 2016
Context queries of a suggester need to be serializes as a dictionary,
not an array.

See #329
olivere added a commit that referenced this issue Jul 27, 2016
Context queries of a suggester need to be serializes as a dictionary,
not an array.

See #329
@olivere
Copy link
Owner

olivere commented Jul 27, 2016

I'm about to release v3.0.45 and v2.0.52 which should fix this issue. Please comment here if that doesn't solve your problem. Thanks for contributing to Elastic.

@olivere olivere closed this as completed Jul 27, 2016
olivere added a commit that referenced this issue Jul 27, 2016
Context queries of a suggester need to be serializes as a dictionary,
not an array.

See #329
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