1

Having a data like this: { "id" : 22, "name" : "test", "tagIds" : [ 2, 35, 56, 59 ] }, how is it possible to search for both name and tags?

Using following query:

{
  "from": 0,
  "size": 100,  
  "query": {
    "terms": {
      "tagIds": [
        35
      ]
    },  
    "multi_match": {
      "query": "test",
      "fields": [
        "name"
      ]
    }
  }
}

Gets this parsing exception: [terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

How to write this correctly in fluent nest?

1 Answer 1

1

You need to use bool query for combining multiple queries. Hence, your query should look like below:

{   
    "from": 0,  
    "size": 100,     
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "tagIds": [
                            35
                        ]
                    }
                },
                {
                    "multi_match": {
                        "query": "test",
                        "fields": [
                            "name"
                        ]
                    }
                }
            ]
        }
    }
}    
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but it doesn't work if tagIds was empty, so I changed must-terms to should-term.
If you change it to should-term, the logic changes. Now the logic becomes (tags IN [35] OR name = test) instead of (tags IN [35] AND name = test). Is that what you want?
Then how can I handle if tags was empty but name wasn't?
This you have to do in the client code itself; Elasticsearch can't help you here much. If tags was empty, then do not even create the query with tags, just create the query with name field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.