Example #1
0
 public function buildSqlWhere()
 {
     $where = $this->config->filter();
     $depth = Status::depth($this->statusVariables);
     if ($depth > 1) {
         if (!$this->config->connectionField()) {
             throw new \Ip\Exception("Nested GRID require 'connectionField' setting to be set.");
         }
         $where .= ' and (' . $where . ') and ' . $this->config->tableName() . '.`' . $this->config->connectionField() . '` = ' . ipDb()->getConnection()->quote($this->statusVariables['gridParentId' . ($depth - 1)]);
     }
     $searchVariables = array();
     foreach ($this->statusVariables as $key => $value) {
         if (preg_match('/^s_/', $key)) {
             $searchVariables[substr($key, 2)] = $value;
         }
     }
     if (!empty($searchVariables)) {
         foreach ($this->config->fields() as $fieldData) {
             if (!empty($fieldData['type']) && $fieldData['type'] == 'Tab') {
                 continue;
             }
             $fieldObject = $this->config->fieldObject($fieldData);
             $fieldQuery = $fieldObject->searchQuery($searchVariables);
             if ($fieldQuery) {
                 if ($where != ' ') {
                     $where .= ' and ';
                 }
                 $where .= $fieldQuery;
             }
         }
     }
     return $where;
 }
Example #2
0
 /**
  * Update document in index for model
  *
  * @param Model $model
  */
 public function update(Model $model)
 {
     // Remove any existing documents for model.
     $this->delete($model);
     // Create new document for model.
     $doc = $this->getDocumentInstance();
     // Add model's class UID.
     list($uidName, $uidValue) = $this->config->classUidPair($model);
     // Add class uid for identification of model's class.
     $doc->setField($uidName, $uidValue);
     list($pkName, $pkValue) = $this->config->primaryKeyPair($model);
     // Add primary key.
     $doc->setField($pkName, $uidValue . '_' . $pkValue);
     //Add id key
     $doc->setField(ModelsConfig::FIELD_LABEL_DEFAULT_DB_PK, $pkValue);
     // Get base fields.
     $fields = $this->config->fields($model);
     // Add fields to document to be indexed (but not stored).
     foreach ($fields as $fieldName => $options) {
         //获取数据库中的值
         $options['value'] = $model->{trim($fieldName)};
         //获取需要索引的数据值
         $value = $this->config()->getSearchFieldValue($options);
         // Get base fields.
         $doc->setField($fieldName, $value);
     }
     $this->index()->add($doc);
 }
 /**
  * A convenient method that aggregates the results of all the other methods in the class. Example of output:
  *
  * <code>
  *    array(
  *        // the keys are names of fields and/or taxonomies
  *        'taxonomy' => array(
  *            'available' => array(
  *                'taxonomy1'    => array(
  *                    'count'    => 10,
  *                    'slug'    => 'taxonomy1',
  *                    'name'    => 'Taxonomy One',
  *                    'font'    => 24
  *                )
  *            ),
  *            'selected' => array(
  *                'taxonomy2'    => array(
  *                    'slug'    => 'taxonomy2',
  *                    'name'    => 'Taxonomy Two'
  *                )
  *            ),
  *            'total' => 10
  *        ),
  *        'rating' => array(
  *            'available' => array(
  *                '10-20' => array(
  *                    'count'    => 4,
  *                    'slug'    => '10-20',
  *                    'to'    => 20,
  *                    'from'    => 10
  *                )
  *            ),
  *            'total' => 4
  *        )
  *    )
  * </code>
  *
  * @param string $minFont The minimum font size to use for display in a tag cloud (defaults to : 12)
  * @param string $maxFont The maximum font size to use for display in a tag cloud (defaults to : 24)
  *
  * @return array An associative array where the keys represent the data point with a list of selected and/or available options.
  **/
 static function all($minFont = 12, $maxFont = 24)
 {
     $options = array();
     foreach (Config::taxonomies() as $tax) {
         $options[$tax] = self::taxonomy($tax);
     }
     $numeric = Config::option('numeric');
     $fields = array_merge(Config::fields(), Config::meta_fields());
     foreach ($fields as $field) {
         if (isset($numeric[$field])) {
             $options[$field] = self::range($field);
         }
         if ($field == 'post_type') {
             $options['post_type'] = self::types(Config::types());
         }
     }
     foreach (Config::customFacets() as $field) {
         $options[$field] = self::custom($field);
     }
     foreach ($options as $name => &$field) {
         if (isset($field['available'])) {
             foreach ($field['available'] as &$available) {
                 $available['font'] = self::cloud($field['available'], $available, $minFont, $maxFont);
             }
         }
     }
     return $options;
 }
 function facets($wp_query, $args)
 {
     $enabled = Config::option('enable_all_posts', false);
     if (!$enabled || !is_post_type_archive() || !isset($wp_query->query_vars['post_type'])) {
         return;
     }
     $fields = Config::fields();
     // should handle this better, good for now
     if (!in_array('post_type', $fields)) {
         die('You must re-index your data with the post_type field enabled to use this ElasticSearch on this post type.');
     }
     $args['post_type'] = $wp_query->query_vars['post_type'];
     return $args;
 }
Example #5
0
 public function searchForm($searchVariables)
 {
     $form = new \Ip\Form();
     $form->setMethod('get');
     $form->addAttribute('autocomplete', 'off');
     $form->removeCsrfCheck();
     foreach ($this->subgridConfig->fields() as $key => $fieldData) {
         if (isset($fieldData['allowSearch']) && !$fieldData['allowSearch']) {
             continue;
         }
         if (!empty($fieldData['type']) && $fieldData['type'] == 'Tab') {
             //tabs (fieldsets)
             $title = '';
             if (!empty($fieldData['label'])) {
                 $title = $fieldData['label'];
             }
             if ($key == 0) {
                 $fieldsets = $form->getFieldsets();
                 $fieldset = $fieldsets[0];
                 $fieldset->setLabel($title);
             } else {
                 $fieldset = new \Ip\Form\Fieldset($title);
                 $form->addFieldset($fieldset);
             }
             $fieldset->addAttribute('id', 'autoGridTabId' . rand(0, 100000000000));
             if ($key == 0) {
                 $fieldset->addAttribute('class', 'tab-pane active');
             } else {
                 $fieldset->addAttribute('class', 'tab-pane');
             }
         } else {
             $fieldObject = $this->subgridConfig->fieldObject($fieldData);
             $field = $fieldObject->searchField($searchVariables);
             if ($field) {
                 $form->addField($field);
             }
         }
     }
     $field = new \Ip\Form\Field\Hidden(array('name' => 'method', 'value' => 'search'));
     $form->addField($field);
     $field = new \Ip\Form\Field\HiddenSubmit();
     $form->addField($field);
     if (count($form->getFieldsets()) > 1) {
         $form->addClass('tab-content');
     }
     return $form;
 }
 /**
  * Add post fields to new elasticsearch object, if the field is set
  *
  * @param WP_Post $post
  * @param Array $document to write to es
  * @return Array $document
  * @internal
  **/
 static function _build_field_values($post, $document)
 {
     foreach (Config::fields() as $field) {
         if (isset($post->{$field})) {
             if ($field == 'post_date') {
                 $document[$field] = date('c', strtotime($post->{$field}));
             } else {
                 if ($field == 'post_content') {
                     $document[$field] = strip_tags($post->{$field});
                 } else {
                     $document[$field] = $post->{$field};
                 }
             }
         }
     }
     return $document;
 }
<?php

namespace elasticsearch;

$fields = array('numeric' => array('id' => 'numeric', 'type' => 'multi_checkbox', 'title' => 'Numeric Fields', 'desc' => 'Any field marked as "numeric" will enabled support for range faceting.', 'options' => array()), 'not_analyzed' => array('id' => 'not_analyzed', 'type' => 'multi_checkbox', 'title' => 'Non Analyzed Fields', 'options' => array(), 'desc' => 'Any string field marked as "non analyzed" will require search terms to match the entire value instead of any words in the value.'));
foreach (array_merge(Config::fields(), Config::meta_fields()) as $field) {
    if ($field != 'post_date' && $field != 'post_type') {
        $fields['numeric']['options'][$field] = $field;
        $fields['not_analyzed']['options'][$field] = $field;
    }
}
$numeric_option = Config::option('numeric');
if ($numeric_option) {
    foreach (array_keys($numeric_option) as $numeric) {
        $fields[$numeric . '_range'] = array('id' => $numeric . '_range', 'type' => 'text', 'title' => $numeric . ' Range', 'desc' => 'Comma delimited list of ranges for this field using the format of FROM-TO. Currently ranges are always inclusive., ie: "-10,10-50,50-" or "-5,6-,7-,8-,9-"');
    }
}
$sections['field'] = array('icon' => NHP_OPTIONS_URL . 'img/glyphicons/glyphicons_097_vector_path_line.png', 'title' => 'Field Mapping', 'desc' => 'Finer grain control over how data is interpreted inside of ElasticSearch. Any changes made in this tab will require you to clear then re-index your data.', 'fields' => $fields);
 /**
  * @internal
  **/
 public static function _buildQuery($search, $facets = array())
 {
     global $blog_id;
     $search = str_ireplace(array(' and ', ' or '), array(' AND ', ' OR '), $search);
     $fields = array();
     $musts = array();
     $filters = array();
     $scored = array();
     foreach (Config::taxonomies() as $tax) {
         if ($search) {
             $score = Config::score('tax', $tax);
             if ($score > 0) {
                 $scored[] = "{$tax}_name^{$score}";
             }
         }
         self::_filterBySelectedFacets($tax, $facets, 'term', $musts, $filters);
     }
     $args = array();
     $numeric = Config::option('numeric');
     $exclude = Config::apply_filters('searcher_query_exclude_fields', array('post_date'));
     $fields = Config::fields();
     self::_searchField($fields, 'field', $exclude, $search, $facets, $musts, $filters, $scored, $numeric);
     self::_searchField(Config::meta_fields(), 'meta', $exclude, $search, $facets, $musts, $filters, $scored, $numeric);
     if (count($scored) > 0 && $search) {
         $qs = array('fields' => $scored, 'query' => $search);
         $fuzzy = Config::option('fuzzy');
         if ($fuzzy && strpos($search, "~") > -1) {
             $qs['fuzzy_min_sim'] = $fuzzy;
         }
         $qs = Config::apply_filters('searcher_query_string', $qs);
         $musts[] = array('query_string' => $qs);
     }
     if (in_array('post_type', $fields)) {
         self::_filterBySelectedFacets('post_type', $facets, 'term', $musts, $filters);
     }
     if (count($filters) > 0) {
         $args['filter']['bool']['should'] = $filters;
     }
     if (count($musts) > 0) {
         $args['query']['bool']['must'] = $musts;
     }
     $args['filter']['bool']['must'][] = array('term' => array('blog_id' => $blog_id));
     $args = Config::apply_filters('searcher_query_pre_facet_filter', $args);
     if (in_array('post_type', $fields)) {
         $args['facets']['post_type']['terms'] = array('field' => 'post_type', 'size' => Config::apply_filters('searcher_query_facet_size', 100));
     }
     // return facets
     foreach (Config::facets() as $facet) {
         $args['facets'][$facet]['terms'] = array('field' => $facet, 'size' => Config::apply_filters('searcher_query_facet_size', 100));
         $args['facets'][$facet]['facet_filter'] = array('bool' => array('must' => array(array('term' => array('blog_id' => $blog_id)))));
         if (count($filters) > 0) {
             $applicable = array();
             foreach ($filters as $filter) {
                 if (isset($filter['term']) && !in_array($facet, array_keys($filter['term']))) {
                     // do not filter on itself when using OR
                     $applicable[] = $filter;
                 }
             }
             if (count($applicable) > 0) {
                 $args['facets'][$facet]['facet_filter']['bool']['should'] = $applicable;
             }
         }
     }
     if (is_array($numeric)) {
         foreach (array_keys($numeric) as $facet) {
             $ranges = Config::ranges($facet);
             if (count($ranges) > 0) {
                 $args['facets'][$facet]['range'][$facet] = array_values($ranges);
                 $args['facets'][$facet]['facet_filter'] = array('bool' => array('must' => array(array('term' => array('blog_id' => $blog_id)))));
             }
         }
     }
     return Config::apply_filters('searcher_query_post_facet_filter', $args);
 }
<?php

namespace elasticsearch;

$fields = array(array('id' => 'fuzzy', 'type' => 'text', 'desc' => 'The number of characters that can be swapped out to match a word. For example; 1 = anoth(a)r~ = anoth(e)r; 2 = (e)noth(a)r~ = (a)noth(e)r; 2 = an()th(u)r~ = an(o)th(e)r. The smaller the number, the better the performance. Leave this blank to disable fuzzy searching. ONLY WORKS FOR VERSIONS > 0.90.1.', 'title' => 'Fuzziness Amount'));
foreach (Config::fields() as $field) {
    $fields[] = array('id' => 'score_field_' . $field, 'type' => 'text', 'validation' => 'numeric', 'desc' => 'A numeric value (if 0, it will have no influence)', 'title' => "Field: {$field}", 'std' => 1);
}
foreach (Config::taxonomies() as $tax) {
    $fields[] = array('id' => 'score_tax_' . $tax, 'type' => 'text', 'validation' => 'numeric', 'desc' => 'A numeric value (if 0, it will have no influence)', 'title' => "Taxonomy: {$tax}", 'std' => 2);
}
foreach (Config::meta_fields() as $field) {
    $fields[] = array('id' => 'score_meta_' . $field, 'type' => 'text', 'validation' => 'numeric', 'desc' => 'A numeric value (if 0, it will have no influence)', 'title' => "Meta: {$field}", 'std' => 3);
}
$sections['scoring'] = array('icon' => NHP_OPTIONS_URL . 'img/glyphicons/glyphicons_079_signal.png', 'title' => 'Result Scoring', 'desc' => 'When executing a search, not all content is created equal. Review each of the items that are indexed and order them by the most relevant/important to least relevant/important.', 'fields' => $fields);
 public function testFieldsFilter()
 {
     add_filter('elasticsearch_config_fields', function () {
         return array('filtered');
     });
     $this->assertEquals(array('filtered'), Config::fields());
 }