# How to map MySQL query to elasticsearch in node.js?

**URL:** https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299
**Category:** Elasticsearch
**Created:** [October 27, 2018, 6:34pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299 "2018-10-27T18:34:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tushar\_Mudgal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_mudgal/32/37009_2.png) [@Tushar\_Mudgal](https://discuss.elastic.co/u/Tushar_Mudgal)
#### Post date: [October 27, 2018, 6:34pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299/1 "2018-10-27T18:34:18Z")

</div>

I am facing an issue with querying. Precisely, I want to extract the results that match my query (astro-ph). How can I query this? It exists in \<primary\_category\> col name in SQL. Column may contain (astro-ph, astro-ph.GC).

I am using elasticsearch.js as the npm library.

```auto
SELECT * FROM paper_metadata WHERE primary_category LIKE 'astro-ph%' ORDER BY DESC;

```

---

<div class="post-metadata">

### Author: ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)
#### Post date: [October 27, 2018, 7:08pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299/2 "2018-10-27T19:08:03Z")

</div>

hi, @Tushar_Mudgal, i didn't use npm library of elasticsearch, but

first you should understand difference of field types: [keyword](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) and [text](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html) .  
If your field, for example, `"prmary category"` is `text field type`(by default), so you can use [term query with sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) .  
keyword and text field types appeared in version [5.5](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_mapping_changes.html)

---

<div class="post-metadata">

### Author: ![Tushar\_Mudgal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_mudgal/32/37009_2.png) [@Tushar\_Mudgal](https://discuss.elastic.co/u/Tushar_Mudgal)
#### Post date: [October 27, 2018, 8:03pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299/3 "2018-10-27T20:03:21Z")

</div>

Hi @nugusbayevkk,  
Here is the example values in my data.  
How can I convert this to Elastic Search query. I want to extract rows with primary\_category -\> astro-ph

```auto
primary_category -> astro-ph
primary_category -> astro-ph.EP
primary_category -> astro-ph.CO

```

---

<div class="post-metadata">

### Author: ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)
#### Post date: [October 27, 2018, 9:13pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299/4 "2018-10-27T21:13:56Z")

</div>

@Tushar_Mudgal , i try to explain how does it work:

1. when you insert data to elasticsearch  
for example word : "astro-ph"  
this word by default divide into 2 term: "astro" and "ph"  
to your index applied ["standard-analyzer"](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html)

so you can use:

1. term query to find documents with terms

> curl -H "Content-type: application/json" localhost:9200/\_search -d '{  
> "query" : { "term" : { "primary\_category" : "astro" } } }'

and as result you get all documents that contains term "astro":

```
{"took":19,"timed_out":false,"_shards":{"total":30,"successful":30,"skipped":0,"failed":0},"hits":{"total":3,"max_score":0.2876821,"hits":[{"_index":"test","_type":"doc","_id":"ntlDt2YBOyk1S2R0H33v","_score":0.2876821,"_source":{
 "primary_category" : "hello astro-ph. and bye"
}},{"_index":"test","_type":"doc","_id":"m9lCt2YBOyk1S2R0_31C","_score":0.2876821,"_source":{
 "primary_category" : "astro-ph"
}},{"_index":"test","_type":"doc","_id":"ndlDt2YBOyk1S2R0H33S","_score":0.2876821,"_source":{
 "primary_category" : "astro-ph.EP"

```

1. or use [match query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)

> curl -H "Content-type: application/json" localhost:9200/\_search -d '{  
> "query" : {  
> "match" : {  
> "primary\_category" : "astro-ph"  
> }  
> }  
> }'

and result:

> {"took":5,"timed\_out":false,"\_shards":{"total":30,"successful":30,"skipped":0,"failed":0},"hits":{"total":3,"max\_score":0.5753642,"hits":[{"\_index":"test","\_type":"doc","\_id":"ntlDt2YBOyk1S2R0H33v","\_score":0.5753642,"\_source":{  
> "primary\_category" : "hello astro-ph. and bye"  
> }},{"\_index":"test","\_type":"doc","\_id":"m9lCt2YBOyk1S2R0\_31C","\_score":0.5753642,"\_source":{  
> "primary\_category" : "astro-ph"  
> }},{"\_index":"test","\_type":"doc","\_id":"ndlDt2YBOyk1S2R0H33S","\_score":0.2876821,"\_source":{  
> "primary\_category" : "astro-ph.EP"

---

<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: [November 24, 2018, 9:24pm UTC](https://discuss.elastic.co/t/how-to-map-mysql-query-to-elasticsearch-in-node-js/154299/5 "2018-11-24T21:24:25Z")

</div>

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