Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved search engine for numeric search queries #428

Closed
wants to merge 2 commits into from

Conversation

javiereguiluz
Copy link
Collaborator

This fixes #422.

}

if (in_array($metadata['dataType'], array('text', 'string'))) {
if (is_numeric($searchQuery)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you add && in_array($metadata['dataType'], array('integer', 'number')) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some changes. I'd appreciate if you could review it again. Thanks!

@Pierstoval
Copy link
Contributor

Seems good to me actually. @ogizanagi ? 😄


if (is_numeric($searchQuery) && $isNumericField) {
$queryConditions->add(sprintf('entity.%s = :exact_query', $name));
$queryParameters['exact_query'] = 0 + $searchQuery; // adding '0' turns the string into a numeric value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this ? 😕 Why is it needed ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more relevant than typecasting it. We could indeed typecast depending on whether it's an integer or a float, but this solution seems good to me.
But yeah, when I saw this, I was like "wtf?", but I think it makes sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding 0 is a trick to convert a numeric string into its numeric counterpat, no matter if it's an integer or float. There is no single PHP function to do this (you must combine intval(), etc.)

If we don't add the 0 and the search query is 56 for example, this is the query I get:

without-adding-zero

If we add the 0 and search again 56, this is the query:

with-adding-zero

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also check whether the "numeric" field type is an integer or a float, and then typecast correctly to avoid comparing a float to an integer, I've just realized that. Is postgresql restrictive in comparing integer and floats ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz : but how is the 'entity.%s = :exact_query' part transformed to WHERE c0_.id IN ('56') in the first screenshot, by juste removing the type converting ?...
My doubt was about the necessity to have the proper type in the bind parameters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I took the first screenshot with another pull request. This is the right screenshot when no 0 is added to the numeric query:

without-adding-zero

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Doctrine doc:

Calling setParameter() automatically infers which type you are setting as value. This works for integers, arrays of strings/integers, DateTime instances and for managed entities.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc is clear ... but it doesn't work like that for me :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz : I think I misunderstand it. I might be tired. Anyway, thanks for the effort you always put into explaining or exposing anything 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the search engine logic
3 participants