Lucene批量Udate问题

时间:2019-03-14 07:08:31

标签: c# lucene lucene.net

下面的代码仅更新documentFields中发送的最后一个项目。因为writer.Dispose()是在foreach循环之后调用的。 注意:documentDto.isUpdate = true;

private void AddFieldsToDocuments(IEnumerable<LuceneDocumentFieldsDto> documentFields)
    {

        //  var analyzer = new CustomAnalyzer();
        // ReSharper disable once LocalVariableHidesMember
        using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
        {
            foreach (var documentDto in documentFields)
            {
                // add data to lucene search index (replaces older entry if any)
                var document = documentDto.document;
                if (documentDto.isUpdate && document == null)
                {
                    document = GetDocument(documentDto.documentId.ToString(), documentDto.documentKeyName);
                }

                if (documentDto.fields?.Count > 0)
                {
                    foreach (var field in documentDto.fields)
                    {
                        document.RemoveField(field.Key);
                        // add lucene fields mapped to db fields
                        // add lucene fields mapped to db fields
                        document = GetDocumentToAdd(document, field);
                    }
                }

                if (document != null)
                {
                    if (!documentDto.isUpdate)
                    {
                        document.Add(new Field(documentDto.documentKeyName, documentDto.documentId.ToString(),
                            Field.Store.YES, Field.Index.ANALYZED));
                        // add entry to index
                        writer.AddDocument(document);
                    }
                    else
                    {
                        var term = new Term(documentDto.documentKeyName, documentDto.documentId.ToString());
                        writer.UpdateDocument(term, document);
                    }
                }

            }

            // close handles
            //   analyzer.Close();
            writer.Dispose();
        }

    }

为解决上述问题,我将writer移到了foreach循环内,并在每个循环上调用writer.dispose(),并用成本解决了问题。现在,它不是批量插入。任何人都可以指出我做错了什么,以便最后可以调用writer.dispose()来实现批量更新。

      private void AddFieldsToDocuments(IEnumerable<LuceneDocumentFieldsDto> documentFields)
    {
        foreach (var documentDto in documentFields)
        {
            // ReSharper disable once LocalVariableHidesMember
            using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)
                var document = documentDto.document;
                if (documentDto.isUpdate && document == null)
                {
                    document = GetDocument(documentDto.documentId.ToString(), documentDto.documentKeyName);
                }

                if (documentDto.fields?.Count > 0)
                {
                    foreach (var field in documentDto.fields)
                    {
                        document.RemoveField(field.Key);
                        // add lucene fields mapped to db fields
                        // add lucene fields mapped to db fields
                        document = GetDocumentToAdd(document, field);
                    }
                }

                if (document != null)
                {
                    if (!documentDto.isUpdate)
                    {
                        document.Add(new Field(documentDto.documentKeyName, documentDto.documentId.ToString(),
                            Field.Store.YES, Field.Index.ANALYZED));
                        // add entry to index
                        writer.AddDocument(document);
                    }
                    else
                    {
                        var term = new Term(documentDto.documentKeyName, documentDto.documentId.ToString());
                        writer.UpdateDocument(term, document);
                    }
                }
                writer.Dispose();
            }
        }
    }

有关更多详细信息,我将发布从上述函数调用的代码:

 protected Document GetDocument(string searchQuery, string searchField = "")
    {
        // set up lucene searcher
        // ReSharper disable once LocalVariableHidesMember
        using (var searcher = new IndexSearcher(directory, true))
        {
            //   var analyzer = new CustomAnalyzer();
            // search by single field
            if (!string.IsNullOrEmpty(searchField))
            {
                var filters = new List<LuceneFilterDto>
                {
                    new LuceneFilterDto
                    {
                        FieldName = searchField,
                        FieldValue = searchQuery,
                        Occur = Occur.MUST,
                        SearchType = LuceneSearchType.WildCardSearch
                    }
                };

                var luceneQueryFilters = new List<LuceneQueryFiltersDto> { new LuceneQueryFiltersDto
                {
                    Condition = Occur.MUST,
                    Filters = filters
                }
                };

                var query = GenerateSearchQuery(luceneQueryFilters, analyzer);

                var hits = searcher.Search(query, int.MaxValue).ScoreDocs;
                if (hits.Length > 0)
                {
                    var docs = hits.Select(elem => searcher.Doc(elem.Doc)).FirstOrDefault();
                    // analyzer.Close();
                    searcher.Dispose();
                    return docs;
                }
            }
        }

        return null;
    }

private Document GetDocumentToAdd(Document document, LuceneFieldDto field)
    {
        // add lucene fields mapped to db fields
        switch (field.FieldTypeId)
        {
            //case (int)Common.FieldType.Number:
            //    if (!string.IsNullOrEmpty(field.Value))
            //        document.Add(new NumericField(field.Key, Field.Store.YES, true).SetIntValue(Convert.ToInt32(field.Value)));
            //    else
            //        document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
            //    break;
            // ReSharper disable once RedundantNameQualifier
            case (int)Common.FieldType.Date:
                if (!string.IsNullOrEmpty(field.Value))
                {
                    if (field.Value != Constants.InvalidDate)
                    {
                        var date = CommonMethods.GetDateTime(field.Value);

                        document.Add(new Field(field.Key,
                            DateTools.DateToString(date, DateTools.Resolution.MILLISECOND),
                            Field.Store.YES, Field.Index.NOT_ANALYZED));
                    }
                }
                else
                    document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
                break;
            default:
                if (field.Value == null)
                {
                    field.Value = string.Empty;
                }
                document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
                break;
        }

        return document;
    }        

0 个答案:

没有答案