2
votes

I have a SQL table that has many records. When I search, I do not want to use all the results, but I want it to be among the ids I want.

I added 'term' to this and added 'addMust' to boolQuery. But there is no result. 'addShould' as I add, without searching for my criteria.

// only the results of the ids in the data_ids array.
$finder = $this->container->get('fos_elastica.finder.xxxxx');
$boolQuery = new \Elastica\Query\BoolQuery();

$fieldQuery = new \Elastica\Query\Match();
$fieldQuery->setFieldQuery('name', $searchKey);
$fieldQuery->setFieldFuzziness('name', 1);
$boolQuery->addShould($fieldQuery);

$termQuery = new \Elastica\Query\Term();
$termQuery->setTerm('id', $data_ids);
$boolQuery->addShould($termQuery); // Without my criteria
//$boolQuery->addMust($termQuery); // No result

$vars = $finder->find($boolQuery, $limit);

Sample !!!

// seaching id's.
$data_ids = [1,2,3,12,13,16,17,24,65,234,657,45];

if i use addMust; always output;

[]

if i use addShould ; sample output;

[
        {
            "station": {
                "id": 8,
                "name": "ETC"
            },
            "area": {
                "id": 1,
                "name": "ETC"
            },
            "city": {
                "id": 2,
                "name": "Istanbul"
            },
            "district": {
                "id": 2,
                "name": "Besiktas"
            }
        }
    ]

but station id 8 is not in the $data_ids array.

How can I solve this problem.

1

1 Answers

1
votes

Try this:

$termsQuery = new \Elastica\Query\Term();
$termsQuery->setTerms('ids', $data_ids); // note: $data_ids is an array
$boolQuery->addMust($termQuery);