# Query index by two fields

**URL:** <https://discuss.elastic.co/t/query-index-by-two-fields/216046>\
**Category:** Logstash\
**Created:** [January 22, 2020, 11:25am UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046 "2020-01-22T11:25:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![namali](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@namali](https://discuss.elastic.co/u/namali)\
**Post date:** [January 22, 2020, 11:25am UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/1 "2020-01-22T11:25:52Z")

</div>

how to find subempcategoryid from emp\_category index

emp\_category index structure  
mainempcategoryid  
mainempcategoryname  
subempcategoryid  
subempcategoryname

used elasticseaxh fillter pluging. but query not working when there two condition  
elasticsearch {  
hosts =\> ["[http://localhost:9200/](http://localhost:9200/)"]  
index =\> "emp\_category"  
query =\> "mainempcategoryid :%{maincategoryid} AND subempcategoryname:%{[data][categories][0]}"  
fields =\> { "subempcategoryid" =\> "subcategoryid"  
}  
}

---

<div class="post-metadata">

**Author:** ![vinu89](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinu89/32/45939_2.png) [@vinu89](https://discuss.elastic.co/u/vinu89)\
**Post date:** [January 22, 2020, 11:36am UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/2 "2020-01-22T11:36:28Z")

</div>

once the index is created in kibana u can view the fields... do you want to see in kibana or json format?

---

<div class="post-metadata">

**Author:** ![namali](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@namali](https://discuss.elastic.co/u/namali)\
**Post date:** [January 22, 2020, 11:39am UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/3 "2020-01-22T11:39:11Z")

</div>

I want this in logstash.

---

<div class="post-metadata">

**Author:** ![vinu89](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinu89/32/45939_2.png) [@vinu89](https://discuss.elastic.co/u/vinu89)\
**Post date:** [January 22, 2020, 11:45am UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/4 "2020-01-22T11:45:01Z")

</div>

no i m not getting what u want exactly u have an index called emp\_category which consists of the mentioned fields.... can u send me the entire config file

---

<div class="post-metadata">

**Author:** ![namali](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@namali](https://discuss.elastic.co/u/namali)\
**Post date:** [January 22, 2020, 12:00pm UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/5 "2020-01-22T12:00:37Z")

</div>

transferring employee data in json to ELK from logstash. while do transfer take employee subcategory id which is already in emp\_category index in ELK. To get subempcategoryid, need to query emp\_category index by mainemocategoryid and subempcategoryname. the query is not working search in two fields . problem is how to get subempcategoryid by querying mainemocategoryid and subempcategoryname

logstash config file  
input {  
file {  
path =\> "/home/......./101229.json"  
start\_position=\>"beginning"  
sincedb\_path =\> "/dev/null"  
}  
}  
filter{  
json { source =\> "message" }  
split { field =\> "data" }  
# empcode  
if ![data][id] {  
mutate {add\_field =\> {"empcode" =\> "null"}}  
} else {  
mutate {add\_field =\> {"empcode" =\> "%{[data][id]}"}}  
}  
# empname  
if [data][name] !="" {  
mutate {add\_field =\> {"empname" =\> "%{[data][name]}"}}  
} else {  
mutate {add\_field =\> {"empname" =\> "null"}}  
}  
elasticsearch {  
hosts =\> ["[http://localhost:9200/](http://localhost:9200/)"]  
index =\> "emp\_maincategory"  
query =\> "empmaincategory:%{[empcategory}"  
fields =\> { "maincategoryid" =\> "mainempcategoryid"  
}  
#empsubcategory  
elasticsearch {  
hosts =\> ["[http://localhost:9200/](http://localhost:9200/)"]  
index =\> "emp\_category"  
query =\> "mainempcategoryid :%{maincategoryid} AND subempcategoryname:%{[data][categories][0]}"  
fields =\> { "subempcategoryid" =\> "subcategoryid"  
}  
}  
}  
output {  
...  
hosts =\> ["[http://localhost:9200/](http://localhost:9200/)"]  
index =\> "emplyee"  
}  
stdout {codec =\> rubydebug}  
}

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [January 22, 2020, 3:45pm UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/6 "2020-01-22T15:45:20Z")

</div>

Why don't you guys ever make a concrete **whole** and **correctly formatted** example?

What is your input data like? What is a sample (as simplified as possible) use case? What do you want to achieve?

A complete post would have the following structure:

Given a file containing jsons structured like

```
{
  "employee_id": "xxx",
  "firstname": "foo",
  "lastname":"bar"
}

```

I'd like to take this file as input in Logstash and ingest the json object as document in an Elasticsearch index `whatever_index`.  
I'd like to add as new documents those events whose "employee\_id" doesn't already exist and update those who already exists.  
And maybe providing a concrete example with 3 input docs, 2 with `employee_id` not already present in the `whatever_index` and one with an `employee_id` already registered but with different fields that need to be updated.

If you spend 10 minutes of your time to make a clear post, you'll help others better understand your needs and help you back.

---

<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:** [February 19, 2020, 3:45pm UTC](https://discuss.elastic.co/t/query-index-by-two-fields/216046/7 "2020-02-19T15:45:24Z")

</div>

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