src/snac/server/elastic/ElasticSearchUtil.php
/**
     * Search Elastic Search with Query
     *
     * Searches the main names index for the given query body.  This is a helper function to condense the codebase.
     *
     * @param string[] $searchBody Associative array of the Elastic Search query body
     * @param integer $start optional The result index to start from (default 0)
     * @param integer $count optional The number of results to return from the start (default 10)
     * @return string[] Results from Elastic Search: total, results list, pagination (num pages), page (current page)
     */
Search Elastic Search with Query
Searches the main names index for the given query body.  This is a helper function to condense the codebase.

@param string[] $searchBody Associative array of the Elastic Search query body
@param integer $start optional The result index to start from (default 0)
@param integer $count optional The number of results to return from the start (default 10)
@return string[] Results from Elastic Search: total, results list, pagination (num pages), page (current page)
Search Elastic Search with Query Searches the main names index for the given query body . This is a helper function to condense the codebase . @param string [] $searchBody Associative array of the Elastic Search query body @param integer $start optional The result index to start from ( default 0 ) @param integer $count optional The number of results to return from the start ( default 10 ) @return string [] Results from Elastic Search : total results list pagination ( num pages ) page ( current page )
private function elasticSearchQuery($searchBody, $start=0, $count=10) {
        $this->logger->addDebug("Searching for a Constellation");
        if (\snac\Config::$USE_ELASTIC_SEARCH) {
            $body = $searchBody;
            $body["from"] = $start;
            $body["size"] = $count;
                $aggs = [
                    "subject"=> [
                        "terms"=> [
                            "field" => "subject",
                            "size" => 10
                        ]
                    ],
                    "occupation"=> [
                        "terms"=> [
                            "field" => "occupation",
                            "size" => 10
                        ]
                    ],
                    "activity"=> [
                        "terms"=> [
                            "field" => "activity",
                            "size" => 10
                        ]
                    ]
                ];
            $body["aggregations"] = $aggs;
            $params = [
                'index' => \snac\Config::$ELASTIC_SEARCH_BASE_INDEX,
                'body' => $body,
                'from' => $start,
                'size' => $count
            ];
            $this->logger->addDebug("Defined parameters for search", $params);
            $results = $this->connector->search($params);
            $this->logger->addDebug("Completed Elastic Search", $results);
            $return = array ();
            foreach ($results["hits"]["hits"] as $i => $val) {
                array_push($return, $val["_source"]);
            }
            $aggregations = array();
            if (isset($results["aggregations"])) {
                foreach($results["aggregations"] as $agg => $vals) {
                    if (!isset($aggregations[$agg]))
                        $aggregations[$agg] = array();
                    if (isset($vals["buckets"])) {
                        foreach ($vals["buckets"] as $term) {
                            array_push($aggregations[$agg], [
                                "term" => $term["key"],
                                "count" => $term["doc_count"]
                            ]);
                        }
                    }
                }
            }
            $response = array();
            $response["total"] = $results["hits"]["total"]["value"];
            $response["results"] = $return;
            $response["aggregations"] = $aggregations;
            if ($response["total"] == 0 || $count == 0) {
                $response["pagination"] = 0;
                $response["page"] = 0;
            } else {
                $response["pagination"] = ceil($response["total"] / $count);
                $response["page"] = floor($start / $count);
            }
            $this->logger->addDebug("Created search response to the user", $response);
            return $response;
        }
        return array (
                    "notice" => "Not Using ElasticSearch"
        );
    }
private function elasticSearchQuery ( $ searchBody , $ start = 0 , $ count = 10 ) { $ this -> logger -> addDebug ( " Searching for a Constellation " ) ; if ( \ snac \ Config :: $ USE_ELASTIC_SEARCH ) { $ body = $ searchBody ; $ body [ " from " ] = $ start ; $ body [ " size " ] = $ count ; $ aggs = [ " subject " => [ " terms " => [ " field " => " subject " , " size " => 10 ] ] , " occupation " => [ " terms " => [ " field " => " occupation " , " size " => 10 ] ] , " activity " => [ " terms " => [ " field " => " activity " , " size " => 10 ] ] ] ; $ body [ " aggregations " ] = $ aggs ; $ params = [ 'index' => \ snac \ Config :: $ ELASTIC_SEARCH_BASE_INDEX , 'body' => $ body , 'from' => $ start , 'size' => $ count ] ; $ this -> logger -> addDebug ( " Defined parameters for search " , $ params ) ; $ results = $ this -> connector -> search ( $ params ) ; $ this -> logger -> addDebug ( " Completed Elastic Search " , $ results ) ; $ return = array ( ) ; foreach ( $ results [ " hits " ] [ " hits " ] as $ i => $ val ) { array_push ( $ return , $ val [ " _source " ] ) ; } $ aggregations = array ( ) ; if ( isset ( $ results [ " aggregations " ] ) ) { foreach ( $ results [ " aggregations " ] as $ agg => $ vals ) { if ( ! isset ( $ aggregations [ $ agg ] ) ) $ aggregations [ $ agg ] = array ( ) ; if ( isset ( $ vals [ " buckets " ] ) ) { foreach ( $ vals [ " buckets " ] as $ term ) { array_push ( $ aggregations [ $ agg ] , [ " term " => $ term [ " key " ] , " count " => $ term [ " doc_count " ] ] ) ; } } } } $ response = array ( ) ; $ response [ " total " ] = $ results [ " hits " ] [ " total " ] [ " value " ] ; $ response [ " results " ] = $ return ; $ response [ " aggregations " ] = $ aggregations ; if ( $ response [ " total " ] == 0 || $ count == 0 ) { $ response [ " pagination " ] = 0 ; $ response [ " page " ] = 0 ; } else { $ response [ " pagination " ] = ceil ( $ response [ " total " ] / $ count ) ; $ response [ " page " ] = floor ( $ start / $ count ) ; } $ this -> logger -> addDebug ( " Created search response to the user " , $ response ) ; return $ response ; } return array ( " notice " => " Not Using ElasticSearch " ) ; }
Search Elastic Search with Query
 Searches the main names index for the given query body.
Search Elastic Search with Query Searches the main names index for the given query body .