# How to get inputs from two tables and store in the same index?

**URL:** <https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574>\
**Category:** Logstash\
**Created:** [October 22, 2019, 2:56am UTC](https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574 "2019-10-22T02:56:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhisheksoni27](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@abhisheksoni27](https://discuss.elastic.co/u/abhisheksoni27)\
**Post date:** [October 22, 2019, 2:56am UTC](https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574/1 "2019-10-22T02:56:37Z")

</div>

I have a posts table that contains some blog posts. Each post has some tags that are stored in some other table.

For example, for post with id 1, that has 5 tags, the post\_tags table has the following entries:

```auto
tagId, postId
1, 1
2, 1
3, 1
4, 1
5, 1

```

The tagId is related to a tag name that can be fethced by doing a join with the tag table

tag table schema:

```auto
id, name

```

post table schema:

```auto
id,
title,
likes,
comments,
published,
userId

```

Now, I want to create a single index that has the following fields:

```auto
postId,
title,
likes,
comments,
published,
userId,
tags

```

(id =\> postId and tags field)

The tags field should be an array of tag names.

How can I achieve this with `jdbc_driver`?

Here's what I have tried:

```auto
    input {
  jdbc {
    jdbc_connection_string => "SKIPPED"
    jdbc_user => "SKIPPED"
    jdbc_password => "SKIPPED"
    jdbc_driver_library => "SKIPPED"
    jdbc_driver_class => "SKIPPED"
    statement => "SELECT post.id as postId, post.likes, post.comments, post.userId, post.title, post.published from post where published > :sql_last_value"
    type => "posts"
    lowercase_column_names => false
  }

  jdbc {
    jdbc_connection_string => "SKIPPED"
    jdbc_user => "SKIPPED"
    jdbc_password => "SKIPPED"
    jdbc_driver_library => "SKIPPED"
    jdbc_driver_class => "SKIPPED"
    statement => "select postId, GROUP_CONCAT(tag.name) as posttags from post_tags, tag, post WHERE post.id = post_tags.postId AND tag.id = post_tags.tagId AND post.published > :sql_last_value GROUP BY post_tags.postId"
    lowercase_column_names => false
    type => "posttags"
  }

}

filter {
 aggregate {
        task_id => "%{postId}"
        code => "
          if (event.get('type') == 'posttags')
            map['posttags'] ||= event.get('posttags').split(',')
            event.cancel()
          end

          if(event.get('type') == 'posts')
            map['postId'] = event.get('postId')
            map['likes'] = event.get('likes')
            map['comments'] = event.get('comments')
            map['userId'] = event.get('userId')
            map['title'] = event.get('title')
            map['published'] = event.get('published')
          end
        "
        push_previous_map_as_event => true
        timeout => 300
     }
}

output {
  stdout { codec => rubydebug }
  elasticsearch {
    document_id => "%{postId}"
    hosts => "localhost"
    index => "posts"
  }
}

```

Or am I just better off writing a script for this?

---

<div class="post-metadata">

**Author:** ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)\
**Post date:** [October 23, 2019, 8:48pm UTC](https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574/2 "2019-10-23T20:48:01Z")

</div>

You need to issue a better SQL query using a UNION function.

---

<div class="post-metadata">

**Author:** ![abhisheksoni27](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@abhisheksoni27](https://discuss.elastic.co/u/abhisheksoni27)\
**Post date:** [October 29, 2019, 5:48am UTC](https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574/3 "2019-10-29T05:48:13Z")

</div>

UNION? I am trying to pull one field from some other table linked by the primary key of the current table.

Can you explain why would I use a UNION for that?

---

<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 26, 2019, 5:52am UTC](https://discuss.elastic.co/t/how-to-get-inputs-from-two-tables-and-store-in-the-same-index/204574/4 "2019-11-26T05:52:13Z")

</div>

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