LibreCat / Catmandu-Store-MongoDB

A searchable store backed by MongoDB
https://metacpan.org/pod/Catmandu::Store::MongoDB
4 stars 2 forks source link

Relations like "<", "<=", ">" and ">=" doesn't work for numbers/number strings #11

Closed jorol closed 5 years ago

jorol commented 5 years ago

Relations like "<", "<=", ">" and ">=" doesn't work for numbers/number strings:

catmandu.yml:

---
store:
  cql:
    package: MongoDB
    options:
      database_name: camel
      bags:
        data:
          cql_mapping:
            default_index: all
            indexes:
              year:
                op:
                  'any': true
                  'all': true
                  '=': true
                  '>': true
                  '>=': true
                  '<': true
                  '<=': true
                  'exact': true
                field: 'year'
...

Store records:

$ catmandu import -v MARC --fix 'marc_map(260c,year);replace_all(year,"\D","");remove_field(record)' to MongoDB --database_name camel < camel.mrc 

Export records:

$ catmandu export -v cql --cql-query 'year < 2000' to YAML
exported 0 items
done

~Fix: Delete 'trick' in /Catmandu/Store/MongoDB/CQL.pm, line 137~

...
# trick to force numeric values interpreted as integers
# $term = $term + 0 if ($term =~ /^[1-9]\d*$/);
...
$ catmandu -I ./fix export -v cql --cql-query 'year < 2000' to YAML
---
_id: 'fol05843555 '
year: '1999'
...
---
_id: 'fol05843579 '
year: '1999'
...
---
_id: 'fol05872355 '
year: '1999'
...
exported 3 items
done

Can someone please cross check this?

jorol commented 5 years ago

Problem is, how values are stored in MongoDB: as string or int. Use fix int() to convert a value to an integer:

$ catmandu import -v MARC --fix 'marc_map(260c,year);replace_all(year,"\D","");int(year);remove_field(record)' to MongoDB --database_name camel < camel.mrc 
imported 10 items
done
catmandu export -v cql --cql-query 'year < 2000' to YAML
---
_id: 'fol05843555 '
year: 1999
...
---
_id: 'fol05843579 '
year: 1999
...
---
_id: 'fol05872355 '
year: 1999
...
exported 3 items
done