Php 当搜索参数为null i Zend Lucene时返回结果

Php 当搜索参数为null i Zend Lucene时返回结果,php,zend-framework,zend-search-lucene,Php,Zend Framework,Zend Search Lucene,我在搜索表单中有多个字段。每个字段都可以为空。 我构建如下查询: $search_title = trim($_POST["search_title"]); $search_skill = trim($_POST["search_skill"]); $search_company = trim($_POST["search_city"]); $search_country_id = trim($_POST["search_county_id"]); $hits = $index->fin

我在搜索表单中有多个字段。每个字段都可以为空。 我构建如下查询:

$search_title = trim($_POST["search_title"]);
$search_skill = trim($_POST["search_skill"]);
$search_company = trim($_POST["search_city"]);
$search_country_id = trim($_POST["search_county_id"]);

$hits = $index->find("title:$search_title and skill:$search_skill and city:$search_city and country_id:$country_id");
if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost)) {
                //get filtered and valid values from search form.
                //filter the array for set values
                $data = array_filter($form->getValues());
                //extract key => vaules as $variable = values
                extract($data);
                 $query1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
                 //test for variable isset and add term to query
                 if (isset($search_title)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_title, 'title'));
                 }
                 if (isset($search_skill)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_skill, 'skill'));
                 }
                 if (isset($search_city)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_city, 'city'));
                 }
                 if (isset($search_country_id)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_country_id, 'country_id'));
                 }
                 //This should give the AND you are looking for...I hope
                 $query= new Zend_Search_Lucene_Search_Query_Boolean(array($query1), array(TRUE));
                 //get result set from query
                 $hits = $index->find($query);
            }
        }
用户只能填写标题、技能或城市等,但如果某个字段为空,我将不会有结果。 只有当所有字段都已填充并匹配时,我才有结果。 如果只填充了一个字段,则不会生成结果,如果为空,则忽略该字段:

$hits = $index->find("title: and skill: and city: and country_id:$country_id");    

您可以尝试以下方法:

$search_title = trim($_POST["search_title"]);
$search_skill = trim($_POST["search_skill"]);
$search_company = trim($_POST["search_city"]);
$search_country_id = trim($_POST["search_county_id"]);

$hits = $index->find("title:$search_title and skill:$search_skill and city:$search_city and country_id:$country_id");
if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost)) {
                //get filtered and valid values from search form.
                //filter the array for set values
                $data = array_filter($form->getValues());
                //extract key => vaules as $variable = values
                extract($data);
                 $query1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
                 //test for variable isset and add term to query
                 if (isset($search_title)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_title, 'title'));
                 }
                 if (isset($search_skill)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_skill, 'skill'));
                 }
                 if (isset($search_city)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_city, 'city'));
                 }
                 if (isset($search_country_id)){
                     $query1->addTerm(new Zend_Search_Lucene_Index_Term($search_country_id, 'country_id'));
                 }
                 //This should give the AND you are looking for...I hope
                 $query= new Zend_Search_Lucene_Search_Query_Boolean(array($query1), array(TRUE));
                 //get result set from query
                 $hits = $index->find($query);
            }
        }
如果在表单中使用
StringTrim
过滤器,则无需对数据使用
trim()
函数。$\u POST数组对于用户提供的数据是危险的,ZF提供了
getValues()
系列方法来提供来自请求对象(POST和GET)的数据,这些方法已经应用了您指定的过滤器和验证器。

我在这个实例中使用了
extract()
函数,因为我使用了
getValues()
,所以数据已经被过滤和验证。当然,还有其他有效的方法将
key=>value
对分配给变量。使用您的最爱。

看看我前面回答的类似问题,了解一些想法:此查询返回全部:搜索标题或搜索技能或搜索城市等,但我需要搜索标题和搜索技能等。