예제 #1
0
	public function testSearch() {

		$client = new Elastica_Client();
		$index = new Elastica_Index($client, 'test');
		$index->create(array(), true);
		$index->getSettings()->setNumberOfReplicas(0);
		//$index->getSettings()->setNumberOfShards(1);

		$type = new Elastica_Type($index, 'helloworldfuzzy');
        $mapping = new Elastica_Type_Mapping($type , array(
               'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'),
               'content' => array('store' => 'yes', 'type' => 'string',  'index' => 'analyzed'),
          ));

        $mapping->setSource(array('enabled' => false));
        $type->setMapping($mapping);
        

		$doc = new Elastica_Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!'));
		$type->addDocument($doc);

		// Refresh index
		$index->refresh();

		$fltQuery = new Elastica_Query_FuzzyLikeThis();
        $fltQuery->setLikeText("sample gmail");
        $fltQuery->addFields(array("email","content"));
        $fltQuery->setMinSimilarity(0.3);
        $fltQuery->setMaxQueryTerms(3);
		$resultSet = $type->search($fltQuery);
		$this->assertEquals(1, $resultSet->count());
	}
예제 #2
0
 /**
  * Handles search and facets.
  *
  * @param string $q
  * @param array $params
  * @param string $type
  * @return Elastica_ResultSet
  * @throws Exception
  */
 public function search($q, $params = array(), $type = 'product')
 {
     if ($this->getStatus()->indexExists($this->_index)) {
         if (empty($params['filters'])) {
             $params['filters'] = '*';
         }
         $queryFilter = new Elastica_Filter_Query(new Elastica_Query_QueryString($params['filters']));
         if (isset($params['range_filters']) && !empty($params['range_filters'])) {
             $andFilter = new Elastica_Filter_And();
             $andFilter->addFilter($queryFilter);
             $filter = new Elastica_Filter_Range();
             foreach ($params['range_filters'] as $field => $rangeFilter) {
                 $filter->addField($field, $rangeFilter);
             }
             $andFilter->addFilter($filter);
             $queryFilter = $andFilter;
         }
         if (empty($q)) {
             $baseQuery = new Elastica_Query_MatchAll();
         } else {
             $baseQuery = new Elastica_Query_Bool();
             if ($this->isFuzzyQueryEnabled()) {
                 $fields = $this->_getSearchFields(true, $q);
                 $queryFuzzy = new Elastica_Query_FuzzyLikeThis();
                 $queryFuzzy->addFields($fields);
                 $queryFuzzy->setLikeText($q);
                 $queryFuzzy->setMinSimilarity($this->getFuzzyMinSimilarity());
                 $queryFuzzy->setPrefixLength($this->getFuzzyPrefixLength());
                 $queryFuzzy->setMaxQueryTerms($this->getFuzzyMaxQueryTerms());
                 $queryFuzzy->setBoost($this->getFuzzyQueryBoost());
                 $baseQuery->addShould($queryFuzzy);
             }
             $queryString = new Elastica_Query_QueryString($q);
             $queryString->setFields($this->_getSearchFields(false, $q));
             $baseQuery->addShould($queryString);
         }
         $filteredQuery = new Elastica_Query_Filtered($baseQuery, $queryFilter);
         $query = Elastica_Query::create($filteredQuery)->setFrom($params['offset'])->setLimit($params['limit']);
         if (isset($params['facets']['queries']) && !empty($params['facets']['queries'])) {
             foreach ($params['facets']['queries'] as $facetQuery) {
                 $facet = new Elastica_Facet_Query($facetQuery);
                 $facet->setParam('query_string', array('query' => $facetQuery));
                 $query->addFacet($facet);
             }
         }
         if (isset($params['stats']['fields']) && !empty($params['stats']['fields'])) {
             foreach ($params['stats']['fields'] as $field) {
                 $facet = new Elastica_Facet_Statistical($field);
                 $facet->setParam('field', $field);
                 $query->addFacet($facet);
             }
         } else {
             if (isset($params['facets']['fields']) && !empty($params['facets']['fields'])) {
                 $properties = $this->_getIndexProperties();
                 foreach ($params['facets']['fields'] as $field) {
                     if (array_key_exists($field, $properties)) {
                         $facet = new Elastica_Facet_Terms($field);
                         if ($properties[$field]['type'] == 'multi_field') {
                             $field .= '.untouched';
                         }
                         $facet->setField($field);
                         $facet->setParam('all_terms', true);
                         $facet->setSize($this->getFacetsMaxSize());
                         $query->addFacet($facet);
                     }
                 }
             }
             if (isset($params['facets']['ranges']) && !empty($params['facets']['ranges'])) {
                 foreach ($params['facets']['ranges'] as $field => $ranges) {
                     $facet = new Elastica_Facet_Range($field);
                     $facet->setField($field);
                     $facet->setRanges($ranges);
                     $query->addFacet($facet);
                 }
             }
         }
         if (isset($params['sort']) && !empty($params['sort'])) {
             foreach ($params['sort'] as $sort) {
                 $query->addSort($sort);
             }
         }
         $result = $this->getIndex($this->_index)->getType($type)->search($query);
         return $result;
     }
     return array();
 }