Lucas
Lucas

Reputation: 367

Mutiple query_strings (nested and not nested)

I have got the following index:

{
   "thread":{
      "properties":{
         "members":{
            "type":"nested",
            "properties":{
               "memberId":{
                  "type":"keyword"
               },
               "firstName":{
                  "type":"keyword",
                  "copy_to":[
                     "members.fullName"
                  ]
               },
               "fullName":{
                  "type":"text"
               },
               "lastName":{
                  "type":"keyword",
                  "copy_to":[
                     "members.fullName"
                  ]
               }
            }
         },
         "name":{
            "type":"text"
         }
      }
   }
}

I want to implement a search, that finds all threads, that either match the members name or the thread name, as long as the user id matches.

My current query looks like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "members",
            "score_mode": "none",
            "query": {
              "bool": {
                "filter": [
                  { "match": { "members.id": "123456789" } }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "members",
            "query": {
              "bool": {
                "must": {
                  "simple_query_string": {
                    "query": "Rhymen",
                    "fields": ["members.fullName"]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

Can I filter the members and thread names in one query or do I have to merge two separate queries? I tried adding a "should" with "minimum_should_match: 1" so I could add a second not nested "query_string". But that didn't work as expected (scores were pretty screwed).

Upvotes: 1

Views: 45

Answers (1)

user3775217
user3775217

Reputation: 4803

yeah i think this should work.

you have to keep the concern for filter memberId in both the filters. Nested filter will need it to match the user with memberId and name.

{
    "query": {
        "bool": {
            "must": [{
                    "nested": {
                        "path": "members",
                        "query": {
                            "term": {
                                "members.memberId": {
                                    "value": 1
                                }
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "should": [{
                                "term": {
                                    "name": {
                                        "value": "thread_name"
                                    }
                                }
                            },
                            {
                                "nested": {
                                    "path": "members",
                                    "query": {
                                        "bool": {
                                            "should": [{
                                                    "term": {
                                                        "members.fullName": {
                                                            "value": "trump"
                                                        }
                                                    }
                                                },
                                                {
                                                    "term": {
                                                        "members.memberId": {
                                                            "value": 1
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Upvotes: 1

Related Questions