# Combining routing and filter

**URL:** <https://discuss.elastic.co/t/combining-routing-and-filter/8347>\
**Category:** Elasticsearch\
**Created:** [July 7, 2012, 1:32am UTC](https://discuss.elastic.co/t/combining-routing-and-filter/8347 "2012-07-07T01:32:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Duarte\_Figuei](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@Daniel\_Duarte\_Figuei](https://discuss.elastic.co/u/Daniel_Duarte_Figuei)\
**Post date:** [July 7, 2012, 1:32am UTC](https://discuss.elastic.co/t/combining-routing-and-filter/8347/1 "2012-07-07T01:32:32Z")

</div>

I am having problems using a terms filter when I specify the routing value.  
I'm indexing documents called "activities" routing by the user's ID. I  
decided changing the default routing since I will always list activities by  
the user ID, so I thought using this field as the routing value would be  
useful. But when I execute the following command:

\*curl  
[http://USER:PASS@192.168.0.1:8080/activity\_index/activity/\_search?routing=17](http://USER:PASS@192.168.0.1:8080/activity_index/activity/_search?routing=17)  
\*

I get documents from more than one user. For example, activities from user  
with ID = 1 are allocated in the same shard as activities from user with ID  
= 6. So, to list only the activities from the user I want, I have to use a  
terms filter, but it is not working.

I am using NEST (ES C# wrapper), so I don't know if it's a bug in NEST, or  
if I am using it wrong, or if it's how elastic search works. Can anyone  
answer me that?

The code I'm using with NEST is:

private static void Index(Activity activity)  
{  
GetClient().Index(activity, activityIndexName, "activity",  
new IndexParameters() { Routing = activity.userID.ToString() });  
}

public static IList ListUserActivities(int userID)  
{  
return GetClient().Search(s =\> s  
.Routing(userID.ToString())  
.Filter(q =\> q  
.Term("userID", userID.ToString()))  
).Documents.ToList();  
}

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 7, 2012, 9:29am UTC](https://discuss.elastic.co/t/combining-routing-and-filter/8347/2 "2012-07-07T09:29:50Z")

</div>

> I have to use a terms filter, but it is not working.

What does a real userID look like, and how is the userID mapped. I'm  
betting that the field is just mapped as { type: "string" }, which means  
that the value is being analyzed, and that your UIDs include capital  
letters, which means that they are being stored lowercase.

A term filter won't work in this situation because you are searching for  
"XYZ123" but the value that is stored is actually "xyz123"

You should set your userID to be:

{ type: "string", index: "not\_analyzed" }

clint

---

<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:** [July 6, 2017, 3:21am UTC](https://discuss.elastic.co/t/combining-routing-and-filter/8347/3 "2017-07-06T03:21:10Z")

</div>

Closed on July 6, 2017, 3:21am UTC


