# ES result to mysql query

**URL:** <https://discuss.elastic.co/t/es-result-to-mysql-query/86234>\
**Category:** Elasticsearch\
**Created:** [May 18, 2017, 10:24am UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234 "2017-05-18T10:24:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![symbios-zi](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@symbios-zi](https://discuss.elastic.co/u/symbios-zi)\
**Post date:** [May 18, 2017, 10:24am UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234/1 "2017-05-18T10:24:45Z")

</div>

Hello there.  
There is a project with MVC framework using MySQL and a products table with description field. I want to use ElasticSearch for full-text search of products.

I have made indexing products to ES and can find products in ElasticSearch. But after that I want still use mysql to get this founded products from Mysql becouse all of products has own relations.

The only one idea I found is getting all ID's of founded results in ES and making query to mysql by this ID's.

It there any another solution to I get data from Mysql using results of ElasticSearch?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 18, 2017, 12:42pm UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234/2 "2017-05-18T12:42:55Z")

</div>

Let say that you have an entity 1 in MySQL.

When you index it in elasticsearch, just use this PK as the document id:

```auto
PUT index/doc/1
{
}

```

Then in the response object, you will get back the `_id` of the 10 first matching documents.  
Just use this id to do a lookup in your DB.

Is that what you are looking at?

---

<div class="post-metadata">

**Author:** ![symbios-zi](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@symbios-zi](https://discuss.elastic.co/u/symbios-zi)\
**Post date:** [May 18, 2017, 12:53pm UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234/3 "2017-05-18T12:53:35Z")

</div>

For example I made full text search and it returns about 200 items. I will get 200 ids from Elastic to array, and make a query to Mysql like

```
SELECT *
FROM products
WHERE id IN (1, 2, 5, 7, 8, 10 ...200);

```

Is it right way? I guess that it will work slow if I will be use 200 IDs in query

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 18, 2017, 1:37pm UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234/4 "2017-05-18T13:37:49Z")

</div>

> I guess that it will work slow if I will be use 200 IDs in query

Probably but that's not an elasticsearch question, right?

If you just want to display results, then reading the elasticsearch `_source` field will be faster I think than doing lookups in another db.

---

<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:** [June 15, 2017, 1:37pm UTC](https://discuss.elastic.co/t/es-result-to-mysql-query/86234/5 "2017-06-15T13:37:54Z")

</div>

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