1

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).

1 Answer 1

1

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
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.