Facing Issue in zend_search_lucene

2019-09-19 01:39发布

问题:

I am using Zend Lucene Search:

    ......
    $results = $test->fetchAll();

                   setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
          Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
                foreach ($results as $result) {
                    $doc = new Zend_Search_Lucene_Document();

                    // add Fields
                    $doc->addField(
                            Zend_Search_Lucene_Field::Text('testid', $result->id));
                    $doc->addField(
                            Zend_Search_Lucene_Field::Keyword('testemail', strtolower(($result->email))));
                    $doc->addField(
                            Zend_Search_Lucene_Field::Text('testconfirmdate', $result->confirmdate));
                    $doc->addField(
                            Zend_Search_Lucene_Field::Text('testcreateddate', $result->createddate));
                    // Add document to the index
                    $index->addDocument($doc);
                }
                // Optimize index.
                $index->optimize();

                // Search by query
                setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
                if(strlen($Data['name']) > 2){
                    //$query = Zend_Search_Lucene_Search_QueryParser::parse($Data['name'].'*');
                                    $pattern = new Zend_Search_Lucene_Index_Term($Data['name'].'*');
                                    $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
                                    $this->view->hits = $index->find(strtolower($query));
                            }
                else{
                    $query  = $Data['name'];
                                    $this->view->hits = $index->find($query);
                                }
............

Works fine here:

  1. It works when I give complete word, first 3 character, case insensitive words

My issues are:

  1. When I search for email, i got error like "Wildcard search is supported only for non-multiple word terms "
  2. When I search for number/date like "1234" or 09/06/2011, I got error like "At least 3 non-wildcard characters are required at the beginning of pattern"

I want to search date, email, number here.

回答1:

In file zend/search/Lucene/search/search/query/wildcard a parameter is set,

private static $_minPrefixLength = 3;

chnage it and it may work..!



回答2:

Based on NaanuManu's suggestion, I did a little more digging to figure this out - I posted my answer on a related question here, but repeating for convenience:

Taken directly from the Zend Reference documentation, you can use:

  • Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() to query the minimum required prefix length and
  • use Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength() to set it.

So my suggestion would be either of two things:

  1. Set the prefixMinLength to 0 using Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0)

  2. Validate all search queries using javascript or otherwise to ensure there is a minimum of Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() before any wildcards used (I recommend querying that instead of assuming the default of "3" so the validation is flexible)