# Facets Search Help

**URL:** <https://discuss.elastic.co/t/facets-search-help/8527>\
**Category:** Elasticsearch\
**Created:** [July 26, 2012, 5:25pm UTC](https://discuss.elastic.co/t/facets-search-help/8527 "2012-07-26T17:25:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat\_2](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pat\_2](https://discuss.elastic.co/u/pat_2)\
**Post date:** [July 26, 2012, 5:25pm UTC](https://discuss.elastic.co/t/facets-search-help/8527/1 "2012-07-26T17:25:59Z")

</div>

I'm having trouble trying to build a proper facets search to accomplish  
what I need. A little bit of background:

So I'm indexing docs daily, that have a created\_timestamp and title\_string  
field (amongst others). What I'm trying to do is to build a query that  
will give me all the docs within a given created\_timestamp range that have  
NEVER been seen before this range based on the title\_string. It is  
important to note that the docs must be broken down daily like this for  
further analysis. I've been trying to do a terms facet with a facet filter  
on the timestamp range like so:

{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"title\_facet" : {  
"terms" : {  
"field" : "title\_string",  
"size" : 10,  
"order" : "reverse\_count",  
}  
"facet\_filter" : {  
"range" : {  
"created\_timestamp" : { "from" : "2012-04-01T00:00:00Z", "to" :  
"2012-04-01T23:59:59Z" }  
}  
}  
}  
}  
}

But this is only giving me the term count based on that given time range,  
not of all time. Using a script inside the terms seemed to have the same  
issue. And if I remove the facet filter, it gives me the term count of all  
time, but then I don't know which terms occur in the time range I'm looking  
for.

Also, is there a way to get the terms facet to return ONLY those with a  
count of 1? It's not as big of a deal, but it would be nice to know.

Sorry if any of this sounds confusing; I can clarify if need be. For  
reference, I'm using version 0.19.2. Any help would be greatly appreciated.

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 26, 2012, 6:12pm UTC](https://discuss.elastic.co/t/facets-search-help/8527/2 "2012-07-26T18:12:45Z")

</div>

Your issue would be understand if you can provide a few example  
documents. A facet over a date range will return a count of all items  
within that range. Why would it be any different? If the count should  
be 1, which documents are being included that should not be? The  
example documents would be helpful. The key piece that is missing is  
the title\_string.

Cheers,

Ivan

On Thu, Jul 26, 2012 at 10:25 AM, Pat [brutus.buckeye@gmail.com](mailto:brutus.buckeye@gmail.com) wrote:

> I'm having trouble trying to build a proper facets search to accomplish what  
> I need. A little bit of background:
> 
> So I'm indexing docs daily, that have a created\_timestamp and title\_string  
> field (amongst others). What I'm trying to do is to build a query that will  
> give me all the docs within a given created\_timestamp range that have NEVER  
> been seen before this range based on the title\_string. It is important to  
> note that the docs must be broken down daily like this for further analysis.  
> I've been trying to do a terms facet with a facet filter on the timestamp  
> range like so:
> 
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "facets" : {  
> "title\_facet" : {  
> "terms" : {  
> "field" : "title\_string",  
> "size" : 10,  
> "order" : "reverse\_count",  
> }  
> "facet\_filter" : {  
> "range" : {  
> "created\_timestamp" : { "from" : "2012-04-01T00:00:00Z", "to" :  
> "2012-04-01T23:59:59Z" }  
> }  
> }  
> }  
> }  
> }
> 
> But this is only giving me the term count based on that given time range,  
> not of all time. Using a script inside the terms seemed to have the same  
> issue. And if I remove the facet filter, it gives me the term count of all  
> time, but then I don't know which terms occur in the time range I'm looking  
> for.
> 
> Also, is there a way to get the terms facet to return ONLY those with a  
> count of 1? It's not as big of a deal, but it would be nice to know.
> 
> Sorry if any of this sounds confusing; I can clarify if need be. For  
> reference, I'm using version 0.19.2. Any help would be greatly appreciated.
> 
> Thanks,  
> Pat

---

<div class="post-metadata">

**Author:** ![pat\_2](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pat\_2](https://discuss.elastic.co/u/pat_2)\
**Post date:** [July 26, 2012, 6:35pm UTC](https://discuss.elastic.co/t/facets-search-help/8527/3 "2012-07-26T18:35:07Z")

</div>

Ivan,

Here's a basic sample. Let's say I have these docs:  
{ "id" : "doc1", "created\_timestamp" : "2012-03-31T01:00:00Z",  
"title\_string" : "title1" },  
{ "id" : "doc2", "created\_timestamp" : "2012-03-31T01:00:00Z",  
"title\_string" : "title2" },  
{ "id" : "doc3", "created\_timestamp" : "2012-03-31T01:00:00Z",  
"title\_string" : "title3" },  
{ "id" : "doc4", "created\_timestamp" : "2012-04-01T02:00:00Z",  
"title\_string" : "title1" },  
{ "id" : "doc5", "created\_timestamp" : "2012-04-01T02:00:00Z",  
"title\_string" : "title4" }

If I do my exact facet I provided earlier, it would return the terms:  
{ "term" : "title1", "count" : 1 },  
{ "term" : "title4", "count" : 1 }

This is NOT what I want because "title1" has been seen before on 03/31. I  
only want "title4" returned since it was first seen in that date range.  
Maybe a faceted search isn't the way to go, but I couldn't think of any  
other way without changing my schema.

-Pat

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 26, 2012, 8:05pm UTC](https://discuss.elastic.co/t/facets-search-help/8527/4 "2012-07-26T20:05:19Z")

</div>

I do not think it is possible to achieve the functionality you are  
looking for. You situation is analogous to having to join two  
identical tables in SQL. You can create a filter that combines the  
range filter with an exists filter, but you cannot dynamically create  
the exists filter for each document.

Perhaps this is doable with scripts. Hopefully someone more familiar  
with deeper levels of scripting will chime in.

Cheers,

Ivan

On Thu, Jul 26, 2012 at 11:35 AM, Pat [brutus.buckeye@gmail.com](mailto:brutus.buckeye@gmail.com) wrote:

> Ivan,
> 
> Here's a basic sample. Let's say I have these docs:  
> { "id" : "doc1", "created\_timestamp" : "2012-03-31T01:00:00Z",  
> "title\_string" : "title1" },  
> { "id" : "doc2", "created\_timestamp" : "2012-03-31T01:00:00Z",  
> "title\_string" : "title2" },  
> { "id" : "doc3", "created\_timestamp" : "2012-03-31T01:00:00Z",  
> "title\_string" : "title3" },  
> { "id" : "doc4", "created\_timestamp" : "2012-04-01T02:00:00Z",  
> "title\_string" : "title1" },  
> { "id" : "doc5", "created\_timestamp" : "2012-04-01T02:00:00Z",  
> "title\_string" : "title4" }
> 
> If I do my exact facet I provided earlier, it would return the terms:  
> { "term" : "title1", "count" : 1 },  
> { "term" : "title4", "count" : 1 }
> 
> This is NOT what I want because "title1" has been seen before on 03/31. I  
> only want "title4" returned since it was first seen in that date range.  
> Maybe a faceted search isn't the way to go, but I couldn't think of any  
> other way without changing my schema.
> 
> -Pat

---

<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:18am UTC](https://discuss.elastic.co/t/facets-search-help/8527/5 "2017-07-06T03:18:54Z")

</div>


