0

I have a question about I can accomplish something. I have my search algorithm for user documents ready.

I get the list of documents, but I don't wanna have the list to have grouped parts of documents of the same user.

Eg:

doc1: user-1 doc2: user-2 doc3: user-2 doc4: user-3 doc5: user-4

Change to:

doc1: user-1 doc2: user-2 doc4: user-3 doc5: user-4 doc3: user-2

Kind of sorting/randomising...

Any tips, ideas for what I can search? Or much better, some examples.

I'm quite new to elastic search. The documentation about custom-scoring or ordering is great but not giving me the right answer.

Thanks a million Stefan

Update 18.08.: As wished, here also my current query.

'query' => [
'filtered' => [
    'query' => [
        'bool' => [
            'must' => [
                'multi_match' => [
                    'query' => $q,
                    'fields' => [ 'title^6', 'description^1', 'tags^3']
                ]
            ], 
            'should' => [
                [
                    'match' => [ 
                        'isTopDocument' => [
                            'query' => 'true',
                            'boost' => 2,
                        ]
                    ]
                ],[
                    'range' => [
                        'online_start' => [
                            'boost' => 1.8,
                            'gte' => 'now-7d/d'
                        ]
                    ]
                ],[
                    'range' => [
                        'online_start' => [
                            'boost' => 1.4,
                            'gte' => 'now-14d/d'
                        ]
                    ]
                ],[ // This is to include all available jobs, at least one should must be true if a must is set
                    // https://www.elastic.co/guide/en/elasticsearch/guide/current/bool-query.html#_controlling_precision
                    'range' => [
                        'online_start' => [
                            'gte' => 'now-61d/d'
                        ]
                    ]
                ]
            ]
        ]
    ],
    'filter' => [
        'bool' => [
            // Some term filters
            'should' => $filter_should,
            'must' => $filter_must,
        ]
    ]
]
],
'size' => $perPage,
'from' => $from
Stefan
  • 362
  • 1
  • 11

1 Answers1

0

Even if you find a search trick to score this particular use-case, you probably want to consider just post-processing the search results to get what you need.

Just loop through the list, keeping a reference to the previous user seen, and if you see the same user in the next result, just remove it from the results, and append it to the end of the list.

Generally speaking you'll get your "shuffled" users as desired, with an occasional pileup of your most prolific user at the very end of the list.

Peter Dixon-Moses
  • 3,169
  • 14
  • 18