コード例 #1
0
        public int Indexing <TObj>(int documentId, TObj obj) where TObj : class
        {
            _documentStorage.Insert <TObj>(documentId, obj);
            NumberOfDocuments++;
            var numberOfTerms = 0;

            foreach (var member in typeof(TObj).GetProperties())
            {
                var fieldName = member.FullName();
                var fieldId   = _fieldDictionary.GetOrCreate(fieldName);
                var attribute = member.GetCustomAttributes(typeof(IndexingAttribute), true).FirstOrDefault();
                if (attribute != null && member.PropertyType == typeof(string))
                {
                    var text = member.GetValue(obj, null) as string;
                    if (!string.IsNullOrEmpty(text))
                    {
                        foreach (var token in _tokinizer.GetTokens(text))
                        {
                            numberOfTerms++;
                            var wordId   = _vocabulary.GetOrAddIndex(token.Term.ToLower());
                            var termInfo = new TermInformation()
                            {
                                TermIndex = token.Index, FieldId = fieldId, StartIndex = 0, StopIndex = 0
                            };
                            _invertedIndex.Insert(wordId, documentId, termInfo);
                        }
                    }
                }
            }
            NumberOfTerms += numberOfTerms;
            _documentNumberOfTerms[documentId] = numberOfTerms;
            return(documentId);
        }
コード例 #2
0
        private int IndexingText(int documentId, string text, string fieldName)
        {
            var numberOfTokens = 0;
            var tokens         = _tokinizer.GetTokens(text);
            var invertedIndex  = _fieldIndex.GetIndexer(fieldName);

            foreach (var token in tokens)
            {
                var wordId   = _vocabulary.GetOrAddIndex(token.Term.ToLower());
                var termInfo = new TermInformation()
                {
                    TermIndex = token.Index, StartIndex = 0, StopIndex = 0
                };
                invertedIndex.Insert(wordId, documentId, termInfo);
                numberOfTokens++;
            }
            return(numberOfTokens);
        }