Exemplo n.º 1
0
        public static SortOption SortField <T>(string propertyName, bool desc = false) where T : ParrotDocument
        {
            LuceneAnalysis analysedField = MetaFinder.PropertyLuceneInfo <LuceneAnalysis>(typeof(T), propertyName);
            LuceneField    storedField   = MetaFinder.PropertyLuceneInfo <LuceneField>(typeof(T), propertyName);
            string         fieldName     = (analysedField != null) ? analysedField.Name : (storedField != null) ? storedField.Name : propertyName;

            return(new SortOption(fieldName, desc));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds Single document to Index
        /// </summary>
        /// <param name="entitydata">Entity with data</param>
        /// <param name="writer">Writer to store data</param>
        private void AddToLuceneIndex(T entitydata, IndexWriter writer)
        {
            // remove older index entry
            LuceneField attranalysis = MetaFinder.PropertyLuceneInfo <LuceneField>(entitydata.GetType(), nameof(entitydata.EntityId));

            //var searchQuery = new TermQuery(new Term(attranalysis.Name, entitydata.EntityId));
            writer.DeleteDocuments(new Term(attranalysis.Name, entitydata.EntityId));
            // add new index entry
            var doc = new Document();

            // add lucene fields
            Indexing(doc, entitydata);
            // add entry to index
            writer.AddDocument(doc);
        }
Exemplo n.º 3
0
        internal static SearchTerm QueryFieldBetween <T>(string propertyName,
                                                         object from,
                                                         object to,
                                                         Occurance occure = Occurance.MUST) where T : ParrotDocument
        {
            string         field    = "";
            LuceneAnalysis analysis = MetaFinder.PropertyLuceneInfo <LuceneAnalysis>(typeof(T), propertyName);

            if (analysis != null)
            {
                field = analysis.Name;
            }
            else
            {
                LuceneField fld = MetaFinder.PropertyLuceneInfo <LuceneField>(typeof(T), propertyName);
                if (fld != null)
                {
                    field = fld.Name;
                }
            }
            if (!string.IsNullOrEmpty(field))
            {
                if (from.GetType() == typeof(DateTime) && to.GetType() == typeof(DateTime))
                {
                    return(new SearchTerm(field, (DateTime)from, (DateTime)to, TermOccurance(occure)));
                }
                else if (from.GetType() == typeof(int) && to.GetType() == typeof(int) ||
                         from.GetType() == typeof(short) && to.GetType() == typeof(short))
                {
                    return(new SearchTerm(field, (int)from, (int)to, TermOccurance(occure)));
                }
                else if ((from.GetType() == typeof(long) && to.GetType() == typeof(long)))
                {
                    return(new SearchTerm(field, (decimal)from, (decimal)to, TermOccurance(occure)));
                }
                else if (from.GetType() == typeof(double) || to.GetType() == typeof(double) ||
                         from.GetType() == typeof(float) || to.GetType() == typeof(float) ||
                         from.GetType() == typeof(decimal) && to.GetType() == typeof(decimal))
                {
                    return(new SearchTerm(field, Convert.ToDouble(from), Convert.ToDouble(to), TermOccurance(occure)));
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Indexing the entity
        /// </summary>
        /// <param name="doc">Lucene document</param>
        /// <param name="lce">Entity to index(annotations are used for indexing)</param>
        internal override void Indexing(Document doc, T lce)
        {
            Type leType = lce.GetType();

            var properties = leType.GetProperties();

            foreach (PropertyInfo pi in properties)
            {
                LuceneAnalysis  attranalysis  = MetaFinder.PropertyLuceneInfo <LuceneAnalysis>(pi);
                LuceneField     fieldanalysis = MetaFinder.PropertyLuceneInfo <LuceneField>(pi);
                LuceneNoIndex   noindex       = MetaFinder.PropertyLuceneInfo <LuceneNoIndex>(pi);
                LuceneGeoIndex  geoIndex      = MetaFinder.PropertyLuceneInfo <LuceneGeoIndex>(pi);
                object          pov           = pi.GetValue(lce);
                ParrotConvertor value         = lce.LuceneConvert(pov);
                if (noindex == null)
                {
                    string fieldName = (attranalysis != null) ? attranalysis.Name : (fieldanalysis != null) ? fieldanalysis.Name : pi.Name;
                    if (value.IsSpatial)
                    {
                        //set strategy for only one field
                        if (geoIndex != null)
                        {
                            AddLocation(doc, value.Phi.Value, value.Lamda.Value);
                        }
                        doc.Add(new StringField(fieldName, $"{value.Phi};{value.Lamda}", Field.Store.YES));
                    }
                    else
                    {
                        if (attranalysis != null)
                        {
                            StoreFieldData(doc, attranalysis.Name, pi.PropertyType, pov, value.Conversion);
                            doc.Add(new TextField(attranalysis.AnalysisName, value.Conversion, Field.Store.YES));
                        }
                        else
                        {
                            StoreFieldData(doc, fieldName, pi.PropertyType, pov, value.Conversion);
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        internal static string FieldName(string propertyName)
        {
            LuceneAnalysis analysis = MetaFinder.PropertyLuceneInfo <LuceneAnalysis>(typeof(T), propertyName);

            if (analysis != null)
            {
                return(analysis.AnalysisName);
            }
            else
            {
                LuceneField fieldanalysis = MetaFinder.PropertyLuceneInfo <LuceneField>(typeof(T), propertyName);
                if (fieldanalysis != null)
                {
                    return(fieldanalysis.Name);
                }
                else
                {
                    return(propertyName);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Maps the document data into the entity
        /// </summary>
        /// <param name="doc">Lucene document</param>
        /// <param name="args">Spatial Arguments</param>
        /// <param name="score">Lucene score</param>
        /// <returns>The Mapped Entity T</returns>
        internal override T MapDocToData(Document doc, SpatialArgs args = null, float?score = null)
        {
            T lce = Activator.CreateInstance <T>();

            if (args != null)
            {
                lce.Distance = DocumentDistance(doc, args);
            }

            Type leType     = lce.GetType();
            var  properties = leType.GetProperties();

            foreach (PropertyInfo pi in properties)
            {
                LuceneNoIndex noindex = MetaFinder.PropertyLuceneInfo <LuceneNoIndex>(pi);
                if (noindex != null)
                {
                    continue;
                }
                LuceneAnalysis analysedField = MetaFinder.PropertyLuceneInfo <LuceneAnalysis>(pi);
                LuceneField    storedField   = MetaFinder.PropertyLuceneInfo <LuceneField>(pi);
                //first we get the analysed field the stored data located in named attribute
                //when the we have a non analysed field we check the stored field defined name else the name is the property name
                string fieldName = (analysedField != null) ? analysedField.Name : (storedField != null) ? storedField.Name : pi.Name;
                object value     = null;
                string svalue;
                if (pi.PropertyType == typeof(GeoCoordinate))
                {
                    svalue = doc.Get($"{fieldName}");
                    var coords = svalue.Split(new char[] { ';' });
                    if (coords.Length > 1)
                    {
                        value = new GeoCoordinate()
                        {
                            Latitude  = ParrotBeak.StringToDouble(coords[0]),
                            Longitude = ParrotBeak.StringToDouble(coords[1])
                        };
                    }
                    else
                    {
                        value = new GeoCoordinate();
                    }
                }
                else
                {
                    svalue = doc.Get(fieldName);
                    if (!string.IsNullOrEmpty(svalue))
                    {
                        if (pi.PropertyType == typeof(string))
                        {
                            value = svalue;
                        }
                        else if (pi.PropertyType == typeof(DateTime))
                        {
                            value = ParrotBeak.DateDeserialize(svalue);
                        }
                        else if (pi.PropertyType == typeof(byte))
                        {
                            value = ParrotBeak.StringToByte(svalue);
                        }
                        else if (pi.PropertyType == typeof(short))
                        {
                            value = ParrotBeak.StringToShort(svalue);
                        }
                        else if (pi.PropertyType == typeof(int))
                        {
                            value = ParrotBeak.StringToInt(svalue);
                        }
                        else if (pi.PropertyType == typeof(long))
                        {
                            value = ParrotBeak.StringToLong(svalue);
                        }
                        else if (pi.PropertyType == typeof(ushort))
                        {
                            value = ParrotBeak.StringToUShort(svalue);
                        }
                        else if (pi.PropertyType == typeof(uint))
                        {
                            value = ParrotBeak.StringToUInt(svalue);
                        }
                        else if (pi.PropertyType == typeof(ulong))
                        {
                            value = ParrotBeak.StringToULong(svalue);
                        }
                        else if (pi.PropertyType == typeof(float))
                        {
                            value = ParrotBeak.StringToFloat(svalue);
                        }
                        else if (pi.PropertyType == typeof(double))
                        {
                            value = ParrotBeak.StringToDouble(svalue);
                        }
                        else if (pi.PropertyType == typeof(decimal))
                        {
                            value = ParrotBeak.StringToDecimal(svalue);
                        }
                        else if (pi.PropertyType == typeof(ParrotId))
                        {
                            value = (ParrotId)svalue;
                        }
                        else
                        {
                            value = JsonConvert.DeserializeObject(svalue);
                        }
                    }
                }
                if (value != null)
                {
                    if (pi.CanWrite)
                    {
                        pi.SetValue(lce, value);
                    }
                }
            }
            return(lce);
        }