How to implement keyword based relevance sorting

Currently my schema looks like this:

type Document {
  id: ID!
  title: String!
  content: String!
  keywords: [String!]! @search
}

I want to do a super simple relavance sorting according to how many keywords an arbitrary document has that match the words in a search. To me the pseudo code sounds simple:

user will input a "searchTerm"

targetKeywords = searchTerm split by spaces

for each Document in the database:
  matches = intersection between Document.keywords and targetKeywords
  Document.count = the length of matches
done

relavantDocuments = sort Documents by count in decending order
return first 10 relavantDocuments

Postgresql and MongoDB both have aggregation pipelines that can do this whole query in on the database as opposed to a server downloading the entire content of the database to do the search. Does anyone know a DGraph oriented method of doing this kind of search? Any information on graphical sorting methods would also be helpful, there’s not much of a community around DGraph.

2 Likes
{
  me(func: Type(Document)) @filter(allofterms(keywords, "All target Keywords here")) {
    count(uid)
  }
}

I was about to work on it, but reading further I noticed that this is really not possible. Dgraph can’t count the number of terms found in a predicate. It counts nodes/edges only.

I think you should do something similar to
https://github.com/dgraph-io/dgraph/issues/3211#issuecomment-505932373

Not saying that your problem is the same, just the example I did would be similar.

2 Likes