private static Field[] AddScoredDoubleField(this LuceneDocument doc, string name, double data)
 {
     return(new Field[2]
     {
         doc.AddDoubleDocValuesField($"{name}$Scoring", data),
         doc.AddStoredField(name, data)
     });
 }
Esempio n. 2
0
        /// <summary>
        /// Indexes the document.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="document">The document.</param>
        /// <param name="indexName">Name of the index.</param>
        /// <param name="mappingType">Type of the mapping.</param>
        public override void IndexDocument <T>(T document, string indexName = null, string mappingType = null)
        {
            try
            {
                Type documentType = document.GetType();
                if (indexName == null)
                {
                    indexName = documentType.Name.ToLower();
                }

                if (mappingType == null)
                {
                    mappingType = documentType.Name.ToLower();
                }

                if (!_indexes.ContainsKey(mappingType))
                {
                    CreateIndex(documentType);
                }

                var index = _indexes[mappingType];

                Document doc = new Document();
                foreach (var typeMappingProperty in index.MappingProperties.Values)
                {
                    TextField textField = new TextField(typeMappingProperty.Name, documentType.GetProperty(typeMappingProperty.Name).GetValue(document, null).ToStringSafe().ToLower(), global::Lucene.Net.Documents.Field.Store.YES);
                    textField.Boost = typeMappingProperty.Boost;
                    doc.Add(textField);
                }

                IndexModelBase docIndexModelBase = document as IndexModelBase;
                string         indexValue        = LuceneID(mappingType, docIndexModelBase.Id);
                doc.AddStringField("type", mappingType, global::Lucene.Net.Documents.Field.Store.YES);
                doc.AddStringField("id", docIndexModelBase.Id.ToString(), global::Lucene.Net.Documents.Field.Store.YES);
                doc.AddStringField("index", indexValue, global::Lucene.Net.Documents.Field.Store.YES);

                // Stores all the properties as JSON to retrieve object on lookup.
                doc.AddStoredField("JSON", document.ToJson());

                // Use the analyzer in fieldAnalyzers if that field is in that dictionary, otherwise use StandardAnalyzer.
                var analyzer = new PerFieldAnalyzerWrapper(defaultAnalyzer: new StandardAnalyzer(_matchVersion, new CharArraySet(_matchVersion, 0, true)), fieldAnalyzers: index.FieldAnalyzers);

                OpenWriter();
                lock ( _lockWriter )
                {
                    if (_writer != null)
                    {
                        _writer.UpdateDocument(new Term("index", indexValue), doc, analyzer);     // Must specify analyzer because the default analyzer that is specified in indexWriterConfig is null.
                    }
                }
            }
            catch (Exception ex)
            {
                HttpContext context2 = HttpContext.Current;
                ExceptionLogService.LogException(ex, context2);
            }
        }