Query data that starts with numbers only

2019-06-03 11:37发布

问题:

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

  • 123Adam- R Test

  • 2Adam Test

  • 534534As

  • ABCSD

  • A person

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>

回答1:

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.



回答2:

     <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>



标签: solr