This paper proposes a Bayesian networks technique and a heuristic search method for retrieving the most relevant documents, by combining the user profiles with the document details in matching the given query words. This proposed technique helps users to retrieve the documents in the descending order relevant to their needs, as the users can specify their requirements through a set of query words of interests along with heuristic information regarding the required documents. This technique stores heuristic information in both document profiles and user profiles. Then Bayesian networks are used to order documents in the descending order of users' relevancy. We have completed the experiment with 850 document records in the faculty library. Experimental results show that there are two factors that might have some effects on the results. The first factor is the sampling of the documents by the system itself and the second factor is the document number relevant to the users' needs.