1

I need to sort result in next order:

  • Users, that I following
  • Users, that follows me
  • All other users

I have users which look like this:

{
    "username": "admin"
    "followers": [
        {
            "id": 2,
            "username": "kiehn.nicola2"
        },
        {
            "id": 3,
            "username": "adaline253"
        },
        {
            "id": 4,
            "username": "skuhic4"
        }
    ],
    "following": [
        {
            "id": 2,
            "username": "kiehn.nicola2"
        },
        {
            "id": 3,
            "username": "adaline253"
        },
        {
            "id": 4,
            "username": "skuhic4"
        },
        {
            "id": 5,
            "username": "heaney.garth5"
         }
    ]
}

Is it possible?

Of course, I know current user id and username.

I write this query, but it doesn't work (for example, user id is 1):

{
    "query": {
        "bool": {
            "must": [
                {
                    "wildcard": {
                        "username": {
                            "value": "*a*",
                            "boost": 1
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "following.username": {
                "order": "asc",
                "nested_path": "following",
                "nested_filter": {
                    "term": {
                        "following.id": 1
                    }
                }
            },
            "followers.username": {
                "order": "asc",
                "nested_path": "followers",
                "nested_filter": {
                    "term": {
                        "followers.id": 1
                    }
                }
            }
        }
    ],
    "size": 40
}

1 Answer 1

1

I would do this by boosting; boost the hits that have the searchers id in their followers by an amount, then boost by a lower value the hits that have the searcher in their 'following' field:

NOTE: the searcher's id is 55 in this example

"query": {
   "bool": {
       "should": [
           {
               "nested": {
                   "path": "followers",
                   "query": {
                       "term" : { "followers.id": { "value": 55, "boost": 3.0 } }
                   }
               }
           },
           {
               "nested": {
                   "path": "following",
                   "query": {
                       "term" : { "following.id": { "value": 55, "boost": 2.0 } }
                   }
               }
           },
           {
               "match_all": { "boost": 1.0 }
           }
       ]           
   }

}

If the searcher is in the hit's followers field, then the searcher is following that hit and so the boost is highest, etc...

You said you wanted all other users, hence the "match_all: {} query at the end.

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.