# PHP client: Check input before indexing

**URL:** <https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511>\
**Category:** Elasticsearch\
**Created:** [February 12, 2018, 4:29pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511 "2018-02-12T16:29:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![syiannop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syiannop/32/26920_2.png) [@syiannop](https://discuss.elastic.co/u/syiannop)\
**Post date:** [February 12, 2018, 4:29pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/1 "2018-02-12T16:29:44Z")

</div>

I am trying to use the PHP client to check if the user's input already exists before indexing it. Here is the [code](https://gist.github.com/sy92/37370975e0d1102532591df6384505f0) I tried.

My idea was:

- Check if the variables are set (I have four required fields: titel, thema, type and description)
- If yes, search in the ES index if they already exist
- If they exist show a message (Should I use do/while instead of íf/else?)
- If the don't, index the input and show a message

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 15, 2018, 3:16pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/2 "2018-02-15T15:16:20Z")

</div>

The flow seems entirely reasonable. You may run into troubles with exact vs partial matches to the search request though.

E.g. if someone searches for `"foo bar baz"` in the `description` field, that will match any documents saying containing `"foo"`, `"bar"`, or `"baz"` since you are using a match query. You may have multiple matches to deal with.

Instead, you may want to use a `term` query, since that performs an exact match. But that also means that case sensitivity matters, as well as spacing and special characters. You could also try using phrase searches but it also has edge-cases.

Basically, search is a bit more nuanced than just a database table lookup, since you need to deal with partial matches.

Perhaps show the results to the user and ask if one of the matching docs was their input? If not they can add it to the index. That saves you a lot of trouble of trying to determine how well partial matches match the input.

---

<div class="post-metadata">

**Author:** ![syiannop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syiannop/32/26920_2.png) [@syiannop](https://discuss.elastic.co/u/syiannop)\
**Post date:** [February 15, 2018, 4:03pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/3 "2018-02-15T16:03:55Z")

</div>

Thank you so much for your answer! I am still at the beginning of the whole process, so I will take notice of your hints and work with them. The problem right now is, that the search part doesn't notice if a document already exists.

E.g: I used the word "test" on title and description and type1/thema1 and added the document to the index. I got the message that the document has been added and its ID. When I used exactly the same input and pressed add again, I got the same response.

I made a few changes to the [code](https://gist.github.com/sy92/37370975e0d1102532591df6384505f0) as I am trying to figure it out.

**EDIT:** I managed to make it work! I will try to improve the search query as you suggested. Any further suggestions are much appreciated 🙂

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 16, 2018, 4:34pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/4 "2018-02-16T16:34:58Z")

</div>

Ah, good to hear you got it working 🙂

No particular advice, just play around with analyzers and get a feel for how they tokenize/transform the text. You may want to do a combination of exact match (`term` queries), phrase matching (`match_phrase` or `phrase` queries) and partial matching (`match`) to suit your needs.

You could also structure your document IDs so that they are deterministic, and use simple GETs instead of searches to fetch the documents by ID. May or may not be a possibility for your system.

---

<div class="post-metadata">

**Author:** ![syiannop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syiannop/32/26920_2.png) [@syiannop](https://discuss.elastic.co/u/syiannop)\
**Post date:** [February 16, 2018, 4:49pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/5 "2018-02-16T16:49:39Z")

</div>

Will do! Thank you for your help 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 16, 2018, 4:49pm UTC](https://discuss.elastic.co/t/php-client-check-input-before-indexing/119511/6 "2018-03-16T16:49:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
