Knowledgebase Home Page  >  SearchUnit  >  Version 2 Articles
Search the Knowledge Base
How can I find the number of matches of a word in a document?
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=109&catId=66

Options

Print this page
Email this to a friend
If you would like to know the number of matches a document (or all documents) has for a particular word, you can achieve this using the data-access layer directly.  In this code we retreive a map of results (which holds the document ID of matching documents and the occurrence IDs in those documents) and use this to find the number of matches (occurrences) for a word in each document.
 
[NOTE: please also see http://keyoti.com/kb/Default.aspx?ToDo=view&questId=117&catId=54 which shows how to show the number of matches INSIDE the ResultItem template in SearchResult control]
 
 
C#
 
using System;
using System.Drawing;
using System.Collections;
using Keyoti.SearchEngine.DataAccess;
using Keyoti.SearchEngine;
....
 
//setup the index directory if necessary
Configuration.xmlLocation = indexDir;
//create a data access instance
XmlDataAccess da = new Keyoti.SearchEngine.DataAccess.XmlDataAccess();
 
//open the data access
da.Open();
 
//retreive a map of the occurrences of "alice" in all the documents in the index
OccurrenceMap resultmap = da.GetMatchingDocumentsAndOccurrencesForWord("alice", new ArrayList(), new Hashtable(), new Keyoti.SearchEngine.Search.SearchOptions());
 
//the map contains document IDs, but we want the actual DocumentRecords for those IDs so we can get the URL of the doc.
ArrayList documentRecords = da.GetDocuments(new ArrayList(resultmap.DocumentIDs));
 
//for every document that has 'alice' in it, get the URL from the docID
foreach(DocumentRecord docRec in documentRecords)
{
    //the URL we might want
    string url = docRec.URI.ToString();
    //the number of matches found in document
    int numberOfOccurrences = resultmap[docRec.DocumentID].Count;
}
da.Close();
 
 
 
VB.NET
 
Imports System
Imports System.Drawing
Imports System.Collections
Imports Keyoti.SearchEngine.DataAccess
Imports Keyoti.SearchEngine
 
 
 
'setup the index directory if necessary
Configuration.xmlLocation = indexDir
'create a data access instance
Dim da = New Keyoti.SearchEngine.DataAccess.XmlDataAccess()
 
'open the data access
da.Open()
 
'retreive a map of the occurrences of "alice" in all the documents in the index
Dim resultmap As OccurrenceMap = da.GetMatchingDocumentsAndOccurrencesForWord("alice", New ArrayList(), New Hashtable(), New Keyoti.SearchEngine.Search.SearchOptions())
 
'the map contains document IDs, but we want the actual DocumentRecords for those IDs so we can get the URL of the doc.
Dim documentRecords As ArrayList = da.GetDocuments(New ArrayList(resultmap.DocumentIDs))
 
'for every document that has 'alice' in it, get the URL from the docID
Dim docRec As DocumentRecord
For Each docRec In  documentRecords
   'the URL we might want
   Dim url As String = docRec.URI.ToString()
   'the number of matches found in document
   Dim numberOfOccurrences As Integer = resultmap(docRec.DocumentID).Count
Next docRec
da.Close()
 
 
 
 
In this example we find all documents with 'alice', we then proceed to count the occurrences (matches) retreived.
 
Tip; you could change the second argument in the call to GetMatchingDocumentsAndOccurrencesForWord to an ArrayList of words that should also match, eg. "alice's", "alices" etc.  The number of occurrences would then relate to the number of occurrences of all of these words.

Related Questions:

Attachments:

No attachments were found.