0

I am creating a sort of forum, and would like to create a function that gives the user suggestions for similar posts before posting a new post, just like in SO. I am not sure how to create this function in the most efficient way?

What will be the parameters to determine a similar post? This needs to be searched by the title of the post only, but still not sure what would be the logic behind it.

Also, I am working with cloud Firestore and I am charged by reads, so that logic needs to be efficient in terms of not needing to read every document in my database to bring up the relevant posts. Should be some smart query.

Would appreciate input and advice on that, thanks!

** Tried to google that many times but the search query brings up unrelated results ("how to create similar posts"). So if there's information out there about that I'd love a link too, couldn't find it myself.

Muhammad Faizan Uddin
  • 1,339
  • 12
  • 29
Tsabary
  • 3,119
  • 2
  • 24
  • 66
  • You can use [Algolia](https://stackoverflow.com/questions/49596610/is-it-possible-to-use-algolia-query-in-firestorerecycleroptions/49607796) to search in your title and description to get similar posts. – Alex Mamo Jun 24 '19 at 07:38
  • @AlexMamo looks interesting! Unfortunately their current Firebase integration is with the realtime database, where I've just moved to Firestore, but maybe I could make it work with Firestore myself somehow. Thanks! – Tsabary Jun 24 '19 at 08:27
  • 1
    It works well with both of them ;) – Alex Mamo Jun 24 '19 at 08:28

1 Answers1

0

You can achieve this by comparing the user input string on each keystroke with the titles stored in database or any other data structures by using a custom made string searching algorithm or by Apache Commons library, which provides efficient algorithms for calculating string similarity.

Levenshtein Distance is one of the popular calculation algorithm, with Levenshtein Distance, the lower the score, the more similar strings are:

StringUtils.getLevenshteinDistance("book", "back") == 2
StringUtils.getLevenshteinDistance("gold", "cold") == 1
StringUtils.getLevenshteinDistance("gold", "coin") == 3
Muhammad Faizan Uddin
  • 1,339
  • 12
  • 29