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

Cleanup Terms query code #1765

Merged
merged 1 commit into from
Mar 12, 2020
Merged

Conversation

thePanz
Copy link
Collaborator

@thePanz thePanz commented Mar 11, 2020

No description provided.

@thePanz thePanz force-pushed the cleanup-terms-query branch 2 times, most recently from 4a616cf to 62ceefb Compare March 11, 2020 16:16
@thePanz thePanz requested a review from ruflin March 11, 2020 16:18
*/
public function setTerms(string $key, array $terms): self
public static function buildTermsLookup(string $field, string $index, string $id, string $path): self
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need this?

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 would say: yes :)
It is an helper function to directly create a Terms lookup query, without first creating the Terms query, and then assign the lookup settings.

WDYT?

Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if the constructor should then allow this? ;-)

But I'm fine going with it.

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'd say that the constructor covers the "base" case, while a specific method can configure the "lookup" settings.
Having an helper doing it in one single go, helps in this direction :)

Saying it here as, if we agree, this could be the standard for other query classes too. wdyt?

Copy link
Owner

Choose a reason for hiding this comment

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

Isn't this the same as:

$terms = (new Terms($field))->setTermsLookup($index, $id, $path);

I did not test if the above works.

What I worry is that we add methods which do not necessarily simplify the code on the user end.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the above is the same of Terms::buildTermsLookup($field, $index, $id, $path)

Copy link
Owner

Choose a reason for hiding this comment

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

Do you think it is still needed? Or could we put the above for example as a doc comment for the constructor?

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 agree, I removed the helper function.
I guess looking at the Terms methods it is clear how to use the setTermsLookup() :)

See: #1767

@thePanz thePanz force-pushed the cleanup-terms-query branch from 62ceefb to 17c3b02 Compare March 12, 2020 10:00
@thePanz thePanz merged commit 80157d7 into ruflin:master Mar 12, 2020
@thePanz thePanz deleted the cleanup-terms-query branch March 12, 2020 10:08
*
* @param string $key Terms key
* @param array $terms Terms list
* @var string[]|null
Copy link
Contributor

Choose a reason for hiding this comment

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

@thePanz @ruflin

Is string[] really the only correct type?

Wouldn't be string[]|int[] be equally correct (and all the other references in this class)?

For ElasticSearch, AFAIK, it doesn't matter if I send a number as string or an actual int, both equally work well.

I'm asking because in the process of the upgrade, using static analyzers, now complains passing integers to these methods but from my experience technically this isn't strictly required?

Thank you

Copy link
Owner

Choose a reason for hiding this comment

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

I haven't tested it but I think you are correct @mfn That both will work. I assume even a double could be passed in and just works? Could you test it?

Copy link
Contributor

Choose a reason for hiding this comment

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

So I did some testing with the terms query directly in the Kibana console and my findings are that string, int and float (i.e. the JavaScript equivalent) work equally well.

E.g. if I've field of type long and I use it in a simple query like this:

GET …/_search
{
    "query": {
      "terms": {
        "some_field": [
          1234
        ]
      }
   }
}

Whether I use "1234", 1234 or 1234.0 did not make any difference.

Note that the same applies to term query too, I tested this:

GET …/_search
{
    "query": {
      "term": {
        "some_field": {
          "value":  1234
        }
      }
   }
}

(Ok realized the Term query class does not use these specific type hints, so not affected by this.)


I think since fields can have the type double, yes, I guess float is also appropriate to include (though not sure how useful a term search for this would be compared to e.g. ranges, but who knows).

Do you think this is good enough warranted a phpdoc change here, i.e. to string[]|int[]|float[]? Annoyingly long btw :)

Copy link
Owner

Choose a reason for hiding this comment

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

@mfn Yes! Can you open a PR?

Copy link
Contributor

@mfn mfn Nov 18, 2020

Choose a reason for hiding this comment

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

Yes! Continuing in #1872

mfn added a commit to mfn/Elastica that referenced this pull request Nov 18, 2020
mfn added a commit to mfn/Elastica that referenced this pull request Nov 18, 2020
mfn added a commit to mfn/Elastica that referenced this pull request Nov 19, 2020
mfn added a commit to mfn/Elastica that referenced this pull request Nov 19, 2020
ruflin pushed a commit that referenced this pull request Nov 20, 2020
…1872)

As shown in #1765 (comment) , ElasticSearch accepts all of them

Note: the change of `public function addTerm` was not previously discussed, but is made for consistency, however poses a breaking change if someone extended this class previously => https://3v4l.org/Ws5bM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants