0

Problem:

I have the following problem. I'm able to search through my data with the SKU. But since there are multiple distributors I want to filter on SKU & distributor. But how can I do that, what I have so far is

PHP

$finder = $this->getContainer()->get('fos_elastica.finder.product.productstock');
$query = new \Elastica\Query\MultiMatch();
$query->setFields(["sku", "distributor"]);
$query->setQuery('XXXXXX');
$productStock = $finder->find($query);
dump($query);
dump($productStock);exit;

$query dump:

Elastica\Query\MultiMatch {#900
  #_params: array:2 [
    "fields" => array:2 [
      0 => "sku"
      1 => "distributor"
    ]
    "query" => "XXXXXXXX"
  ]
  #_rawParams: []
}

Query result:

array:2 [
  0 => AppBundle\Entity\ProductStock {#970
    -id: 16218
    -sku: "XXXXXX"
    -stock: 90
    -price: "11.00"
    -distributor: "XXX"
  }
  1 => AppBundle\Entity\ProductStock {#938
    -id: 54086
    -sku: "XXXXXX"
    -stock: 25
    -priceOverride: "11.00"
    -distributor: "XXX"   
  }
]

Database table ProductStock

+---+-----+------+--------+-------+
|id | sku |stock | vendor | price |
+---+-----+------+--------+-------+

My goal what I want to achieve is to get 1 array from my Elasticsearch result if the SKU and distributor has a match.

1 Answer 1

1

You need to set type of the query :

$query->setType(self::TYPE_BEST_FIELDS);
$query->setOperator(self::OPERATOR_AND);

If this not work :

$query->setType(self::TYPE_MOST_FIELDS);

Explanation :

The most_fields type is most useful when querying multiple fields that contain the same text analyzed in different way

check more details here

Sign up to request clarification or add additional context in comments.

3 Comments

This still gives me multiple results which is not the idea. It should return 1 result since sku and distributor matches only 1 distributor.
add $query->setOperator(self::OPERATOR_AND);
Thx for your response and help so far. But the result from this is not the expected result. SKU & Distributor are two different columns in the database and If I am not wrong its not possible to search for both fields with two queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.