Elastica过滤器和查询阵列

时间:2015-08-19 20:25:08

标签: elastica

我正在使用表单进行高级搜索。表单输入表示elasticsearch索引中的数据。我的模型会收到一系列过滤条件和查询字词。

$data = array(
  'Filter' => array(
    'FilerId' => 14592
  ),
  'Query' => array(
    'FiledDate' => '2015-08-06',
  ),
);

我正在使用foreach循环来创建过滤器和查询

foreach ($data['Filter'] AS $field => $value) {
    $filter = new \Elastica\Filter\Term();
    $filter->setTerm($field, $value); 
    $filterArray[] = $filter;

}

foreach ($data['Query'] AS $field => $value) {
    $query = new \Elastica\Query\QueryString($value);
    $query->setDefaultOperator('AND')
          ->setDefaultField($field);
    $queryArray[] = $query;

}

$query = new \Elastica\Query();
$query
    ->setFields(['TranId'])
    ->setQuery($queryArray)
    ->setFilter($filterArray);

$search->setQuery($query);


$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
    foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
        $results = $resultSet->getResults();
        $totalResults = $resultSet->getTotalHits();

        foreach ($results as $result) {
            $fields = $result->getFields('TransId');
            $transid[] = $fields['TranId'][0];
        }    // ... handle Elastica\ResultSet
    }
    $comma_separated = implode(", ", $transid);
}
return array('transactions' => $comma_separated, 'total' => $totalResults);

我收到错误,我找不到原因?

这是一些更新的代码。我得到了结果,但没有得到我认为应该得到的结果。

$boolFilter = new \Elastica\Filter\BoolFilter();

foreach ($data['Filter'] AS $field => $value) {
    $term = new \Elastica\Filter\Term();
    $term->setTerm($field, $value); 
    $boolFilter->addMust($term);
}

$boolQuery = new \Elastica\Query\BoolQuery();

foreach ($data['Query'] AS $field => $value) {
    $match = new \Elastica\Query\Match();
    $match->setFieldQuery($field, $value)
          ->setFieldAnalyzer($field, 'whitespace')
          ->setFieldOperator($field, 'AND'); 
    $boolQuery->addMust($match);
}

$query = new \Elastica\Query();
$query
    ->setFields(['TranId'])
    ->setQuery($boolQuery)
    ->setFilter($boolFilter);
//print $error->getError();
print "<pre>";
print_r ($query->toArray());
print "</pre>";
$search->setQuery($query);

$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
    foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
        $results = $resultSet->getResults();
        $totalResults = $resultSet->getTotalHits();

        foreach ($results as $result) {
            $fields = $result->getFields('TransId');
            $transid[] = $fields['TranId'][0];
        }    // ... handle Elastica\ResultSet
    }
    $comma_separated = implode(", ", $transid);
    }
    return array('transactions' => $comma_separated, 'total' => $totalResults);

1 个答案:

答案 0 :(得分:1)

好的!我需要添加标准分析仪而不是空白分析仪。空白分析器可以工作但只能在空格上打破短语。标准分析仪打破了这个短语,使案例更低。

https://www.elastic.co/guide/en/elasticsearch/guide/current/analysis-intro.html#analyze-api

$boolFilter = new \Elastica\Filter\BoolFilter();

foreach ($data['Filter'] AS $field => $value) {
    $term = new \Elastica\Filter\Term();
    $term->setTerm($field, $value); 
    $boolFilter->addMust($term);
}

$boolQuery = new \Elastica\Query\BoolQuery();

foreach ($data['Query'] AS $field => $value) {
    $match = new \Elastica\Query\Match($value);
    $match->setFieldQuery($field, $value); 
    $match->setFieldAnalyzer($field, 'standard');
    $boolQuery->addMust($match);
}

$filterQuery = new \Elastica\Query\Filtered();
$filterQuery->setFilter($boolFilter);
$filterQuery->setQuery($boolQuery);

$query = new \Elastica\Query($filterQuery);
$query->setFields(['TranId']);

print "<pre>";
print_r ($query->toArray());
print "</pre>";

$search->setQuery($query);

$numberOfEntries = $search->count();
$comma_separated = 0;
if ($numberOfEntries) {
    foreach ($search->scanAndScroll() as $scrollId => $resultSet) {
        $results = $resultSet->getResults();
        $totalResults = $resultSet->getTotalHits();

        foreach ($results as $result) {
            $fields = $result->getFields('TransId');
            $transid[] = $fields['TranId'][0];
        }    // ... handle Elastica\ResultSet
    }
    $comma_separated = implode(", ", $transid);
    }
    return array('transactions' => $comma_separated, 'total' => $totalResults);