user357086
user357086

Reputation: 404

Query data that starts with numbers only

Hello I have a field in Solr that start with 1 or more digits. For e.g

How can I configure a field type, such that it will match any data that starts with numbers only? Please suggest.

I created this type but it is other way around it strips the numbers leaves the characters for e.g data "123ABCSD" It end up "ABCSD". I would like to issue query q=0:9 or somefield:. should return rows that starts with numbers only.

<fieldType name="numbersfirst" class="solr.TextField">
    <analyzer>
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.PatternReplaceFilterFactory" pattern="^[0-9]+([^[0-9]]*)" replacement="" replace="all"/>
    </analyzer>
</fieldType>

Upvotes: 1

Views: 712

Answers (2)

user357086
user357086

Reputation: 404

     <script>
    <![CDATA[
            function findInfoStartingWithNumber(row)    {

                var patternStr = "^[0-9]+([^[0-9]]*)";
                var pattern = java.util.regex.Pattern.compile(patternStr);
                var inputStr = (row.get('name') == null ? "" : row.get('name').toString().trim().toUpperCase());
                var matcher = pattern.matcher(inputStr);
                var matchFound = matcher.find(); // false                   
                row.put('StartsWithNumber',matchFound);

                return row;
            }
    ]]>
</script>
<entity name="MaterialInfo" transformer="script:findInfoStartingWithNumber" query="">

In case anybody wants to know where the above elements fall under the db-data-config.xml element structure

<dataconfig>
 <datasource>
  <script>
  </script>
  <document>
  <entity></entity>
 </document>
</datasource>

Upvotes: 0

brian519
brian519

Reputation: 318

One way to do this is to create a separate boolean field that just stores whether your field starts with a number. Then when you're indexing, just parse the field yourself and set your boolean field accordingly. Then it's a simple query against that boolean field.

Upvotes: 1

Related Questions