Sitecore 7 ContentSearch API with numeric POCO properties in the filter / where condition not working

StackOverflow https://stackoverflow.com/questions/21084293

  •  27-09-2022
  •  | 
  •  

Question

I have been spending few hours on this issue now, but no luck so far. So reaching out to the community for help.

I have a data template called Product with a field called ProPrice, and some content based on this template I have enabled all fields to be indexed using the configuration <indexAllFields>true</indexAllFields>. When I rebuild the index, I see the index field (proprice) along with the terms stored in Lucene correctly (verified using Luke).

Now I use Sitecore 7 ContentSearch API to fetch content from Lucene index. And for this, I have created a POCO entity called Product, which inherits from SearchResultItem, and have also added a property for price as below:

[IndexField("proprice")]
public double Price { get; set; }

However the following LINQ query does not return any data:

var products = context.GetQueryable<Product>().Where(p => p.Price == 4.0).ToList();

When I look at Sitecore search log, the Lucene query that this translated to was - proprice:[4 TO 4]. And if I execute this query directly against the index in Luke, it returns data. I tried this with other conditions such as p.Price >= 1.0, but none worked. What is interesting is - when I remove the condition and get all records, the Price property in the Product entity is populated with the correct double value (4.0).

But if I change the query slightly as follows, it returns correct data:

var products = context.GetQueryable<Product>().Where(p => p["proprice"] == "4").ToList();

So it looks like when the condition is against numeric values, it does not work. And unfortunately, I need to filter based on a numeric range, and hence the above approach will not work for me. I can avoid ContentSearch API and directly execute the Lucene query using the Lucene provider, but I will not be able to switch to a different search provider, such as Solr, in future. Or alternatively, I can get all data, and then filter in my code - I wouldn't prefer that though.

I would appreciate any help in resolving this. PS: Few other points: 1) I tried the field type of "ProPrice" in the data template as single line text, integer, double - none worked 2) Am using the default Lucene analyzer - Lucene.Net.Analysis.Standard.StandardAnalyzer

Était-ce utile?

La solution 2

I think you need the type converter attribute adding to the field. I've done this in my projects and filtering on numbers has worked fine:

So change the property to:

[IndexField("proprice")]
[TypeConverter(typeof(IndexFieldNumberValueConverter))]
public double Price { get; set; }

And you should be able to filter.

Autres conseils

You need to map your index field type to system.double as follows (Notice type="System.Double"):

<field fieldName="proprice"       storageType="YES" indexType="TOKENIZED"    vectorType="NO" boost="1f" type="System.Double" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
                <analyzer type="[YOUR ANALYZER]" />
</field>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top