Search code examples
foselasticabundle

How to get only ids or one field with fos elastica


What is the fastest way to get _ids of a certain index with fosElasticaBundle (3.1.0) ?? My index has around 30,000 documents.

For example I would get the list of all Ids with fosElastica. Equivalent Elasticsearch:

    curl http://localhost:9200/search/etablissement/_search?pretty=true -d { '
        "query" : { 
            "match_all" : {} 
        },
        "fields": []
    }
    '

Or if I want to get name :

    curl http://localhost:9200/search/etablissement/_search?pretty=true -d { '
        "query" : { 
            "match_all" : {} 
        },
        "fields": ['name']
    }
    '

Thank you


Solution

  • Finally I found an answer to my question. I just wonder if this is the right way.

        $elasticaService = $this->container->get('fos_elastica.index.search');
        $elasticaService->request("_search", "GET",  array("fields"=>array()), array("match_all" => array(),"size" => 999999) )
    
        $i = 1;
        $data = array();
        if(isset($results["hits"]["hits"])){
            foreach($results["hits"]["hits"] as $result){
                $data[$i] = $result["_id"];
                ++$i;
            }
        }
        return $data;
    

    To avoid a mistake I had to add :

    "index.max_result_window: 999999" to /etc/elasticsearch/elasticsearch.yml and "service apache2 restart" to reload configuration.