3

Any idea why ElasticSearch would always return a _score of 0 for all the search queries i do ?

Using Elastica, i am doing something like:

$elasticaClient= $this->getElasticaClient();
$elasticaIndex  = $elasticaClient->getIndex($this->getIndexName());   
$elasticaQuery  = new Elastica\Query\BoolQuery(); 

$queryAnd = new \Elastica\Query\BoolQuery();
$queryOr = new \Elastica\Query\BoolQuery();

$queryOr->addShould(new \Elastica\Query\Wildcard('search_field1', $keyword));
$queryOr->addShould(new \Elastica\Query\Wildcard('search_field2', $keyword));

$queryAnd->addMust($queryOr);
$elasticaQuery->addFilter($queryAnd);

$mainQuery = new \Elastica\Query();
$mainQuery->setQuery($elasticaQuery);

$elasticaResultSet = $elasticaIndex->search($mainQuery);

I get a bunch of results back, but always the _score for those results is 0, even if i enter the full word that can be found in the stored field(for a full match).

The mapping for the field is pretty simple:

'search_field1' => array(
    'type'              => 'string',
    'include_in_all'    => true,
    'analyzer'          => 'stringLowercase',
),

The stringLowercase analyzer is just:

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

Moreover, even if i try to boost either of the fields, it does not seem to have any effect.

Can anybody shed some light over this?

Twisted1919
  • 2,430
  • 1
  • 19
  • 30
  • Can you share a bit more of your client code and/or query? It's hard to draw conclusions with limited information. – Val Dec 26 '16 at 19:36
  • Can you call `setExplain(true)` on your query and update your question with the explanation result you get? – Val Dec 27 '16 at 05:52
  • You probably have some filter wrapping everything and resetting the scores. – Andrei Stefan Dec 27 '16 at 07:21
  • @AndreiStefan - Thanks, i was using the addFilter() method all over the place, changing to addMust seems to have kept the score and now sorting works exactly as expected. – Twisted1919 Jan 03 '17 at 14:22
  • @Val - Thanks, i updated the code a bit and while doing this i saw AndreiStefan suggestion and noticed i am using a filter to wrap everything. Changed to use addMust instead of addFilter and everything started to work like a charm so far. Any downside here for using addMust instead of addFilter ? – Twisted1919 Jan 03 '17 at 14:24
  • With `addMust` you'll keep the scores, with `addFilter` you'll discard the scores, but other than both work the same way, i.e. they AND your constraints together. Basically you can remove the `addFilter` line and only keep `$elasticaQuery->addMust($queryOr);` – Val Jan 03 '17 at 14:28
  • this is good to find out, i didn't know this aspect, thank you. – Twisted1919 Jan 03 '17 at 14:30

1 Answers1

0

have you tried to query something nested like this?:

//i.e.
$queryField = new \Elastica\Query\QueryString($query);
$queryField->setDefaultOperator('OR');
$queryBool->addMustNot($queryField);
//then 
$queryOr = new \Elastica\Query\Wildcard('search_field1', $keyword);
$queryOr->addShould($queryBool);
T.Todua
  • 53,146
  • 19
  • 236
  • 237