How to convert a string field into int in a Lucene query?

1,840 views
Skip to first unread message

Nima Ha

unread,
Sep 8, 2014, 9:16:43 AM9/8/14
to rav...@googlegroups.com

I am working with a bunch of documents in RavenDB that have an Age field that has to be an int but unfortunately they have been stored as string. I want to write a query to get all the docs with ages more than 20 here's what I do :

Age : [20 TO *]

But I am getting docs with ages of 3 to 9 too.(strings are compared from left hand side)

The question is how can I tell Lucene to treat age as int ?

(This system is somehow a legacy system I can't change the Age type in documents)

Tobi

unread,
Sep 8, 2014, 10:10:07 AM9/8/14
to rav...@googlegroups.com
In your static index you can do something like:

new { Age = int.Parse(doc.Age) }

(assuming doc.Age is always a valid int)

Tobias

Am 08.09.2014 um 15:16 schrieb Nima Ha:

> I am working with a bunch of documents in |RavenDB| that have an
> |Age| field that has to be an |int| but unfortunately they have been
> stored as |string|. I want to write a query to get all the docs with
> ages more than 20 here's what I do :
>
> |Age : [20 TO*]|
>
> But I am getting docs with ages of |3 to 9| too.(strings are compared
> from left hand side)
>
> The question is how can I tell |Lucene| to treat age as |int| ?
>
> (This system is somehow a legacy system I can't change the Age type in
> documents)
>
> --
> You received this message because you are subscribed to the Google
> Groups "RavenDB - 2nd generation document database" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ravendb+u...@googlegroups.com
> <mailto:ravendb+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Grisha Kotler

unread,
Sep 8, 2014, 10:35:40 AM9/8/14
to rav...@googlegroups.com
Your index should look something like:
from doc in docs.Age
select new {
Age = Int32.Parse(doc.Age)
}

And your query:
Age_Range:[Ix20 TO NULL]


Grisha Kotler

RavenDB Core Team

Tel: 972-4-6227811

Fax: 972-153-4-6227811




--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Sep 8, 2014, 12:01:14 PM9/8/14
to ravendb
What does your document looks like?



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

Fax:      + 972-153-4622-7811





--

Nima Ha

unread,
Sep 9, 2014, 7:14:15 AM9/9/14
to rav...@googlegroups.com
My document looks like this :

31d2c0e8-4ea9-44ab-9e93-af628c115d0e
{
  ,
  "IDNo": {
    "Name": "IDNo",
    "Value": "1392141",
    "Desc": "1392141",
    "DataType": "System.String",
    "IsRunning": false
  },
  "NationalID": {
    "Name": "NationalID",
    "Value": "8607578049",
    "Desc": "8607578049",
    "DataType": "System.String",
    "IsRunning": false
  },
  "AllowPublish": {
    "Name": "AllowPublish",
    "Value": "AllowPublish",
    "DataType": "System.String",
    "IsRunning": false
  },
  "AdContactNumber": {
    "Name": "AdContactNumber",
    "Value": "CellPhonCh1",
    "DataType": "System.Collections.Generic.List`1[System.Object]",
    "IsRunning": false
  },
  "ContactDayofWeek": {
    "Name": "ContactDayofWeek",
    "Value": "ContactDayofWeek1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "ContactTimeFrom": {
    "Name": "ContactTimeFrom",
    "Value": "ContactTimeFrom1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "ContactTimeTo": {
    "Name": "ContactTimeTo",
    "Value": "ContactTimeTo1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "AdContactNumber2": null,
  "PropertyType": {
    "Name": "PropertyType",
    "Value": "Apartman",
    "DataType": "System.String",
    "IsRunning": true
  },
  "Usability": null,
  "TransactionType": {
    "Name": "TransactionType",
    "Value": "Frosh",
    "DataType": "System.String",
    "IsRunning": true
  },
  "PostalCode": {
    "Name": "PostalCode",
    "Value": "1372879695",
    "Desc": "1372879695",
"DataType": "System.String",
    "IsRunning": false
  },
  
  "Street": null,
  "PropertyTypeT": null,
  "TransactT": null,
  "Floor": {
    "Name": "Floor",
    "Value": "0",
    "Desc": "0",
"DataType": "System.String",
    "IsRunning": false
  },
  "AgeOfBuilding": {
    "Name": "AgeOfBuilding",
    "Value": "57",
    "Desc": "57",
    "DataType": "System.String",
    "IsRunning": true
  },
  "Kaf": null,
  "Balcony": null,
  "Basement": null,
  "StorageT": null,
  "Campus": null,
  "TotalPriceA": {
    "Name": "TotalPriceA",
    "Value": "1161534851",
    "Desc": "1161534851",
    "DataType": "System.String",
    "IsRunning": false
  },
  "M2Price": {
    "Name": "M2Price",
    "Value": "49504575",
    "Desc": "49504575",
    "DataType": "System.String",
    "IsRunning": true
  },
  "LoanA": {
    "Name": "LoanA",
    "Value": "671699724",
    "Desc": "671699724",
    "DataType": "System.String",
    "IsRunning": false
  },
  "TrustT": null,
  "NetPriceA": {
    "Name": "NetPriceA",
    "Value": "1994039412",
    "Desc": "1994039412",
    "DataType": "System.String",
    "IsRunning": false
  },
  "SaleType": {
    "Name": "SaleType",
    "Value": "SaleType1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "ForceMajor": null,
  "TotalPricePish": null,
  "Loan": null,
  "NetPrice": null,
  "PishPardakht": null,
  "UnitPardakht": null,
  "Trust": null,
  "MinTrust": null,
  "MaxTrust": null,
  "Rent": null,
  "MinRent": null,
  "MaxRent": null,
  "Adaptable": null,
  "GeneralDescription": null,
  "DescriptionT": null,
  "DescriptionPish": null,
  "SaleSpecification": {
    "Name": "SaleSpecification",
    "Value": "SaleSpecification3",
    "DataType": "System.String",
    "IsRunning": false
  },
  "Feature": {
    "Name": "Feature",
    "Value": "Feature1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "Application": {
    "Name": "Application",
    "Value": "Application4",
    "DataType": "System.String",
    "IsRunning": false
  },
  "Room": {
    "Name": "Room",
    "Value": "Room6",
    "DataType": "System.String",
    "IsRunning": false
  },
  "BuildingArea": {
    "Name": "BuildingArea",
    "Value": "86288",
    "Desc": "86288",
    "DataType": "System.String",
    "IsRunning": false
  },
  "TrustA": null,
  "GeneralSpecification": {
    "Name": "GeneralSpecification",
    "Value": "Exceptional",
    "DataType": "System.String",
    "IsRunning": false
  },
  "DesignSpecification": {
    "Name": "DesignSpecification",
    "Value": "DesignSpecification3",
    "DataType": "System.String",
    "IsRunning": false
  },
  "RentSpecification": null,
  "PishSpecification": null,
  "CountSide": null,
  "SizeSide1": null,
  "SizeSide2": null,
  "SizeSide3": null,
  "CountDahaneh": null,
  "HasResident": {
    "Name": "HasResident",
    "Value": "HasResident1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "Status": null,
  "PropertyStatus": null,
  "DeedStatus": {
    "Name": "DeedStatus",
    "Value": "PropertyStatus2",
    "DataType": "System.String",
    "IsRunning": false
  },
  "LocalPosition": {
    "Name": "LocalPosition",
    "Value": "LocalPosition2",
    "DataType": "System.String",
    "IsRunning": false
  },
  "LocatedIn": null,
  "NameOfMojtama": null,
  "PossibleUseType": null,
  "Facade": {
    "Name": "Facade",
    "Value": "Facade8",
    "DataType": "System.String",
    "IsRunning": false
  },
  "Direction": {
    "Name": "Direction",
    "Value": "South",
    "DataType": "System.Collections.Generic.List`1[System.Object]",
    "IsRunning": false
  },
  "CountSideA": {
    "Name": "CountSideA",
    "Value": "CountSideA1",
    "DataType": "System.String",
    "IsRunning": false
  },
  "UnitPerFloor": {
    "Name": "UnitPerFloor",
    "Value": "UnitPerFloor3",
    "DataType": "System.String",
    "IsRunning": false
  },
  "OwnerResidence": null,
  "Skylight": {
    "Name": "Skylight",
    "Value": "Skylight2",
    "DataType": "System.String",
    "IsRunning": false
  },
  "PropertyStatusA": {
    "Name": "PropertyStatusA",
    "Value": "PropertyStatus3",
    "DataType": "System.String",
    "IsRunning": false
  },
  "TotalFloor": {
    "Name": "TotalFloor",
    "Value": "36",
    "Desc": "36",
    "DataType": "System.String",
    "IsRunning": false
  },
  "PossibleUseTypeA": {
    "Name": "PossibleUseTypeA",
    "Value": "PossibleUseTypeA2",
    "DataType": "System.Collections.Generic.List`1[System.Object]",
    "IsRunning": false
  },
  "View": {
    "Name": "View",
    "Value": "View6",
    "DataType": "System.Collections.Generic.List`1[System.Object]",
    "IsRunning": false
  },
  "LocatedInA": {
    "Name": "LocatedInA",
    "Value": "LocatedInA4",
    "DataType": "System.String",
    "IsRunning": false
  },
  "FloorInUnit": null,
  "Electricity": {
    "Name": "Electricity",
    "Value": "Electricity1",
    "DataType": "System.Collections.Generic.List`1[System.Object]",
    "IsRunning": false
  },
  "CountElectricity1": null,
  "CountElectricity2": null,
  "ElectricityStatus": null,
  "Tel": null,
  "Gas": null,
  "WaterSupply": null,
  "WaterHeating": null,
  "CoolingSystem": null,
  "HeatingSystem": null,
  "Toilet": null,
  "GId": "31d2c0e8-4ea9-44ab-9e93-af628c115d0e",
  "OwnerID": "4fd46297-6e25-43ac-9a27-f21aae659471",
  "OwnerGroupID": null,
  "LastModifierID": "4fd46297-6e25-43ac-9a27-f21aae659471",
  "LastModifyDate": "2013-12-15T15:54:20.8248797",
  "InsertDate": "2013-12-15T15:54:20.8248797",
  "Version": 3,
  "Title": null,
  "Code": null,
  "DeleteFlag": false
}

Oren Eini (Ayende Rahien)

unread,
Sep 9, 2014, 7:21:41 AM9/9/14
to ravendb
You have actually stored the value as a string, so you'll need to use int.Parse, yes.



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

Fax:      + 972-153-4622-7811





--
Reply all
Reply to author
Forward
0 new messages