# Making order count

**URL:** <https://discuss.elastic.co/t/making-order-count/4007>\
**Category:** Elasticsearch\
**Created:** [March 1, 2011, 8:21pm UTC](https://discuss.elastic.co/t/making-order-count/4007 "2011-03-01T20:21:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evil\_faery](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@evil\_faery](https://discuss.elastic.co/u/evil_faery)\
**Post date:** [March 1, 2011, 8:21pm UTC](https://discuss.elastic.co/t/making-order-count/4007/1 "2011-03-01T20:21:12Z")

</div>

Hello everyone.

Im new to ES, could you tell me if I can do next thing with it.

Case: I have a number of lists like:  
#1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
#2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
#3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
#4 {item1: "ttt", item2:"www", item3:"xxx"}

I want to find lists that contain aaa,bbb,ccc But the tricky part is  
that I want to take order into consideration as well. So that even if  
I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
most relevant result, and sometimes #2.

Thanks.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [March 1, 2011, 9:45pm UTC](https://discuss.elastic.co/t/making-order-count/4007/2 "2011-03-01T21:45:22Z")

</div>

I would index the doc as you wrote it (3 fields item1, item2 and  
item3) and then you you give every field a different boost depending  
on your needs:

item1:text1^boost1 AND item2:text2^boost2 AND item3:text3^boost2 ...

Regards,  
Peter.

--  
[http://jetwick.com](http://jetwick.com) Personalized Twitter Search

On 1 Mrz., 21:21, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:

> Hello everyone.
> 
> Im new to ES, could you tell me if I can do next thing with it.
> 
> Case: I have a number of lists like:  
> #1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
> #2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
> #3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
> #4 {item1: "ttt", item2:"www", item3:"xxx"}
> 
> I want to find lists that contain aaa,bbb,ccc But the tricky part is  
> that I want to take order into consideration as well. So that even if  
> I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
> most relevant result, and sometimes #2.
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![evil\_faery](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@evil\_faery](https://discuss.elastic.co/u/evil_faery)\
**Post date:** [March 1, 2011, 10:58pm UTC](https://discuss.elastic.co/t/making-order-count/4007/3 "2011-03-01T22:58:10Z")

</div>

Hi Peter

thanks for your response. I understood your idea, but Im afraid I  
didnt fully specify what I need in original post.

The thing is that even though im interested in order, it is  
secondary. So if I search the above lists for aaa,bbb,ccc I want to  
get #1, #2 and #3.

Where #3 is in the results because it has "aaa" (with lowest score),  
#2 because it has all of them (bbb,ccc,aaa) and #1 is highest rated  
because it has all of them too AND in the correct order.

Nick.

On Mar 1, 11:45 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> I would index the doc as you wrote it (3 fields item1, item2 and  
> item3) and then you you give every field a different boost depending  
> on your needs:
> 
> item1:text1^boost1 AND item2:text2^boost2 AND item3:text3^boost2 ...
> 
> Regards,  
> Peter.
> 
> --[http://jetwick.comPersonalized](http://jetwick.comPersonalized) Twitter Search
> 
> On 1 Mrz., 21:21, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > Hello everyone.
> 
> > Im new to ES, could you tell me if I can do next thing with it.
> 
> > Case: I have a number of lists like:  
> > #1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
> > #2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
> > #3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
> > #4 {item1: "ttt", item2:"www", item3:"xxx"}
> 
> > I want to find lists that contain aaa,bbb,ccc But the tricky part is  
> > that I want to take order into consideration as well. So that even if  
> > I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
> > most relevant result, and sometimes #2.
> 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![cwho](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cwho](https://discuss.elastic.co/u/cwho)\
**Post date:** [March 2, 2011, 2:49am UTC](https://discuss.elastic.co/t/making-order-count/4007/4 "2011-03-02T02:49:54Z")

</div>

I've done something similar in Lucene before, you want to use one  
field for the list, e.g.:

item: "aaa bbb ccc"  
item: "bbb aaa ccc"  
item: "vvv aaa zzz"

with an analyzer (e.g. whitespace) so each term gets indexed (or you  
can try ES lists, which I haven't tried).

Your query for "aaa bbb ccc" with order weighting will look like:

(aaa OR bbb OR ccc) OR "aaa bbb ccc"

the first part matches anything that has one of the three terms - with  
higher relevance for more matches, while the second part boosts the  
ones that have them in the correct contiguous order. You can use a  
fuzzy phrase query instead of a phrase query in the second part if you  
want some form of noncontiguous order matches.

On Mar 2, 6:58 am, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:

> Hi Peter
> 
> thanks for your response. I understood your idea, but Im afraid I  
> didnt fully specify what I need in original post.
> 
> The thing is that even though im interested in order, it is  
> secondary. So if I search the above lists for aaa,bbb,ccc I want to  
> get #1, #2 and #3.
> 
> Where #3 is in the results because it has "aaa" (with lowest score),  
> #2 because it has all of them (bbb,ccc,aaa) and #1 is highest rated  
> because it has all of them too AND in the correct order.
> 
> Nick.
> 
> On Mar 1, 11:45 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > I would index the doc as you wrote it (3 fields item1, item2 and  
> > item3) and then you you give every field a different boost depending  
> > on your needs:
> 
> > item1:text1^boost1 AND item2:text2^boost2 AND item3:text3^boost2 ...
> 
> > Regards,  
> > Peter.
> 
> > --[http://jetwick.comPersonalizedTwitter](http://jetwick.comPersonalizedTwitter) Search
> 
> > On 1 Mrz., 21:21, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > > Hello everyone.
> 
> > > Im new to ES, could you tell me if I can do next thing with it.
> 
> > > Case: I have a number of lists like:  
> > > #1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
> > > #2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
> > > #3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
> > > #4 {item1: "ttt", item2:"www", item3:"xxx"}
> 
> > > I want to find lists that contain aaa,bbb,ccc But the tricky part is  
> > > that I want to take order into consideration as well. So that even if  
> > > I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
> > > most relevant result, and sometimes #2.
> 
> > > Thanks.

---

<div class="post-metadata">

**Author:** ![evil\_faery](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@evil\_faery](https://discuss.elastic.co/u/evil_faery)\
**Post date:** [March 2, 2011, 9:55pm UTC](https://discuss.elastic.co/t/making-order-count/4007/5 "2011-03-02T21:55:51Z")

</div>

Thanks for your suggestion, however that does not solve case where  
order match will be partial.

For example I search for aaa, bbb, ccc and have these lists:

#5 zzz, aaa, bbb  
#6 aaa, zzz, bbb

In this case i would want #6 to be ranked higher, because "aaa"  
position is as in the query - on first place.

On Mar 2, 4:49 am, cwho80 [fuzzyb...@gmail.com](mailto:fuzzyb...@gmail.com) wrote:

> I've done something similar in Lucene before, you want to use one  
> field for the list, e.g.:
> 
> item: "aaa bbb ccc"  
> item: "bbb aaa ccc"  
> item: "vvv aaa zzz"
> 
> with an analyzer (e.g. whitespace) so each term gets indexed (or you  
> can try ES lists, which I haven't tried).
> 
> Your query for "aaa bbb ccc" with order weighting will look like:
> 
> (aaa OR bbb OR ccc) OR "aaa bbb ccc"
> 
> the first part matches anything that has one of the three terms - with  
> higher relevance for more matches, while the second part boosts the  
> ones that have them in the correct contiguous order. You can use a  
> fuzzy phrase query instead of a phrase query in the second part if you  
> want some form of noncontiguous order matches.
> 
> On Mar 2, 6:58 am, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > Hi Peter
> 
> > thanks for your response. I understood your idea, but Im afraid I  
> > didnt fully specify what I need in original post.
> 
> > The thing is that even though im interested in order, it is  
> > secondary. So if I search the above lists for aaa,bbb,ccc I want to  
> > get #1, #2 and #3.
> 
> > Where #3 is in the results because it has "aaa" (with lowest score),  
> > #2 because it has all of them (bbb,ccc,aaa) and #1 is highest rated  
> > because it has all of them too AND in the correct order.
> 
> > Nick.
> 
> > On Mar 1, 11:45 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > > I would index the doc as you wrote it (3 fields item1, item2 and  
> > > item3) and then you you give every field a different boost depending  
> > > on your needs:
> 
> > > item1:text1^boost1 AND item2:text2^boost2 AND item3:text3^boost2 ...
> 
> > > Regards,  
> > > Peter.
> 
> > > --[http://jetwick.comPersonalizedTwitterSearch](http://jetwick.comPersonalizedTwitterSearch)
> 
> > > On 1 Mrz., 21:21, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > > > Hello everyone.
> 
> > > > Im new to ES, could you tell me if I can do next thing with it.
> 
> > > > Case: I have a number of lists like:  
> > > > #1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
> > > > #2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
> > > > #3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
> > > > #4 {item1: "ttt", item2:"www", item3:"xxx"}
> 
> > > > I want to find lists that contain aaa,bbb,ccc But the tricky part is  
> > > > that I want to take order into consideration as well. So that even if  
> > > > I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
> > > > most relevant result, and sometimes #2.
> 
> > > > Thanks.

---

<div class="post-metadata">

**Author:** ![cwho](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cwho](https://discuss.elastic.co/u/cwho)\
**Post date:** [March 6, 2011, 1:42pm UTC](https://discuss.elastic.co/t/making-order-count/4007/6 "2011-03-06T13:42:52Z")

</div>

arguably in the above case, aaa and bbb are closer in #5 and should be  
higher.

If you want to weight specific relations, try a bigram query.

Now put a "start of line" token at the start (other may have  
experience using term vectors to get the same result):  
so #5 is "start zzz aaa bbb"  
and #6 is "start aaa zzz bbb"

then your bigram search for "aaa bbb ccc" is:  
aaa OR bbb OR ccc OR ("start aaa") OR ("aaa bbb") OR ("bbb ccc")

Now in the above case you've specified that you want aaa to be in the  
first position more than you want aaa to be next to bbb, this can be  
tweaked by boosting the various bigram terms:  
aaa OR bbb OR ccc OR ("start aaa")^1.5 OR ("aaa bbb") OR ("bbb ccc")

now the "start aaa" bigram term is weighted up, so results where aaa  
is in the first term is now higher. Do experiment on your own to find  
the fit for your case, but you get the drift....

On Mar 3, 5:55 am, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:

> Thanks for your suggestion, however that does not solve case where  
> order match will be partial.
> 
> For example I search for aaa, bbb, ccc and have these lists:
> 
> #5 zzz, aaa, bbb  
> #6 aaa, zzz, bbb
> 
> In this case i would want #6 to be ranked higher, because "aaa"  
> position is as in the query - on first place.
> 
> On Mar 2, 4:49 am, cwho80 [fuzzyb...@gmail.com](mailto:fuzzyb...@gmail.com) wrote:
> 
> > I've done something similar in Lucene before, you want to use one  
> > field for the list, e.g.:
> 
> > item: "aaa bbb ccc"  
> > item: "bbb aaa ccc"  
> > item: "vvv aaa zzz"
> 
> > with an analyzer (e.g. whitespace) so each term gets indexed (or you  
> > can try ES lists, which I haven't tried).
> 
> > Your query for "aaa bbb ccc" with order weighting will look like:
> 
> > (aaa OR bbb OR ccc) OR "aaa bbb ccc"
> 
> > the first part matches anything that has one of the three terms - with  
> > higher relevance for more matches, while the second part boosts the  
> > ones that have them in the correct contiguous order. You can use a  
> > fuzzy phrase query instead of a phrase query in the second part if you  
> > want some form of noncontiguous order matches.
> 
> > On Mar 2, 6:58 am, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > > Hi Peter
> 
> > > thanks for your response. I understood your idea, but Im afraid I  
> > > didnt fully specify what I need in original post.
> 
> > > The thing is that even though im interested in order, it is  
> > > secondary. So if I search the above lists for aaa,bbb,ccc I want to  
> > > get #1, #2 and #3.
> 
> > > Where #3 is in the results because it has "aaa" (with lowest score),  
> > > #2 because it has all of them (bbb,ccc,aaa) and #1 is highest rated  
> > > because it has all of them too AND in the correct order.
> 
> > > Nick.
> 
> > > On Mar 1, 11:45 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > > > I would index the doc as you wrote it (3 fields item1, item2 and  
> > > > item3) and then you you give every field a different boost depending  
> > > > on your needs:
> 
> > > > item1:text1^boost1 AND item2:text2^boost2 AND item3:text3^boost2 ...
> 
> > > > Regards,  
> > > > Peter.
> 
> > > > --[http://jetwick.comPersonalizedTwitterSearch](http://jetwick.comPersonalizedTwitterSearch)
> 
> > > > On 1 Mrz., 21:21, evil faery [evil.faeryta...@gmail.com](mailto:evil.faeryta...@gmail.com) wrote:
> 
> > > > > Hello everyone.
> 
> > > > > Im new to ES, could you tell me if I can do next thing with it.
> 
> > > > > Case: I have a number of lists like:  
> > > > > #1 {item1: "aaa", item2:"bbb", item3:"ccc"}  
> > > > > #2 {item1: "bbb", item2:"ccc", item3:"aaa"}  
> > > > > #3 {item1: "vvv", item2:"aaa", item3:"zzz"}  
> > > > > #4 {item1: "ttt", item2:"www", item3:"xxx"}
> 
> > > > > I want to find lists that contain aaa,bbb,ccc But the tricky part is  
> > > > > that I want to take order into consideration as well. So that even if  
> > > > > I need lists with aaa,bbb,ccc, sometimes I would want to get #1 as  
> > > > > most relevant result, and sometimes #2.
> 
> > > > > Thanks.

---

<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, 4:10am UTC](https://discuss.elastic.co/t/making-order-count/4007/7 "2017-07-06T04:10:43Z")

</div>


