Domanda

I am testing out Elastica and Elastic Search. I am trying to add a filter to my query that only returns results by city location. It is returning empty. I've tried filter by username, and so on and it always returns empty, so it would seem my understanding isn't quite correct. Here's my code to analyse, map, then search with a filter

    $elasticaIndex = $elasticaClient->getIndex('users');
    // Create the index new
    $elasticaIndex->create(
            array(
        'analysis' => array(
            'analyzer' => array(
                'indexAnalyzer' => array(
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => array('lowercase', 'lb_ngram')
                ),
                'searchAnalyzer' => array(
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => array('standard', 'lowercase', 'lb_ngram')
                )
            ),
            'filter' => array(
                'lb_ngram' => array(
                    "max_gram" => 10,
                    "min_gram" => 1,
                    "type" => "nGram"
                )
            )
        )
            ), true
    );
    //Create a type
    $elasticaType = $elasticaIndex->getType('profile');


    // Set mapping
    $mapping->setProperties(array(
        //'id' => array('type' => 'integer', 'include_in_all' => FALSE),
        'firstName' => array('type' => 'string', 'include_in_all' => TRUE),
        'lastName' => array('type' => 'string', 'include_in_all' => TRUE),
        'username' => array('type' => 'string', 'include_in_all' => TRUE),
        'bio' => array('type' => 'string', 'include_in_all' => TRUE),
        'thumbnail' => array('type' => 'string', 'include_in_all' => FALSE),
        'location' => array('type' => 'string', 'include_in_all' => TRUE),
    ));

..... Then to search, I do the following

        $elasticaQueryString = new Elastica\Query\QueryString();

        //'And' or 'Or' default : 'Or'
        $elasticaQueryString->setDefaultOperator('AND');
        $elasticaQueryString->setQuery($term);

        // Create the actual search object with some data.
        $elasticaQuery = new Elastica\Query();
        $elasticaQuery->setQuery($elasticaQueryString);

To add a filter

            $elasticaFilterLocation = new \Elastica\Filter\Term();
            //search 'location' = $region;
            $elasticaFilterLocation->setTerm('location', $region);           
            $elasticaQuery->setFilter($elasticaFilterLocation);
            $elasticaResultSet = $elasticaIndex->search($elasticaQuery);                
            $elasticaResults = $elasticaResultSet->getResults();

If I comment out the filter, I do get the expected results. What am I missing? Does it have to do with the analyzer or mapping?

È stato utile?

Soluzione

I had the same issue, I fixed it by changing my analyzers for these fields in the mappings. Now I have to rebuild my indexes etc... Don't try to find a solution to change mapping or analyzer without rebuilding your index, you can't.

lc_analyzer' => array(
   'type' => 'custom',
   'tokenizer' => 'keyword',
   'filter' => array('lowercase')
),

For mapping:

'location' => array('type' => 'string', 'analyzer' => 'lc_analyzer'),

Then, query like

 $elasticaFilterBool = new \Elastica\Filter\Bool();                
 $filter1 = new \Elastica\Filter\Term();
 $filter1->setTerm('location', array(strtolower($region)));                
 $elasticaFilterBool->addMust($filter1);
 $elasticaQuery->setFilter($elasticaFilterBool);

I think your location is being tokenized on spaces and commas so it is killing the search. should solve it for you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top