# Please suggest how to achieve following with elasticsearch

**URL:** <https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877>\
**Category:** Elasticsearch\
**Created:** [February 25, 2013, 10:35am UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877 "2013-02-25T10:35:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyaank\_Choubey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyaank_choubey/32/2470_2.png) [@Priyaank\_Choubey](https://discuss.elastic.co/u/Priyaank_Choubey)\
**Post date:** [February 25, 2013, 10:35am UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877/1 "2013-02-25T10:35:23Z")

</div>

Hi all,

I have a unique problem to solve, need your suggestion to solve that.

Index Requirement :

1. i need to index response with multiple titles with one unique response  
id.

Search Requirement:

1. i need to search some response title text and need to return say 30 or  
some defined count of response id with the most matched response title
2. return will only have response Id and most matched title.

Please suggest how should i index and create a query for the same.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 25, 2013, 10:57am UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877/2 "2013-02-25T10:57:10Z")

</div>

Hiya

I suggest providing some examples of what your documents might look like  
and what the search results might look like. That'd make it easier to  
make suggestions about how to design a solution

clint

> I have a unique problem to solve, need your suggestion to solve that.
> 
> Index Requirement :
> 
> 1. i need to index response with multiple titles with one unique  
> response id.
> 
> Search Requirement:
> 
> 1. i need to search some response title text and need to return say 30  
> or some defined count of response id with the most matched response  
> title
> 2. return will only have response Id and most matched title.
> 
> Please suggest how should i index and create a query for the same.
> 
> --  
> You received this message because you are subscribed to the Google  
> Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Priyaank\_Choubey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyaank_choubey/32/2470_2.png) [@Priyaank\_Choubey](https://discuss.elastic.co/u/Priyaank_Choubey)\
**Post date:** [February 25, 2013, 11:36am UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877/3 "2013-02-25T11:36:01Z")

</div>

Three sample document

{  
response\_id : "1001",  
response\_title: ["response title 1"," response title 2 hello "]  
}

{  
response\_id : "1002",  
response\_title: ["Hello response title 1 Hello hello"," response title 2  
hello "]  
}

{  
response\_id : "1003",  
response\_title: ["response title 1"," response title 2"]  
}

if i search Hello i need output

[  
{response\_id:"1001", response: "response title 2 hello"},  
{response\_id : "1002", "Hello response title 1 Hello hello"} \*\* Return only  
one title here with the most matched title only  
]

On Monday, February 25, 2013 4:27:10 PM UTC+5:30, Clinton Gormley wrote:

> Hiya
> 
> I suggest providing some examples of what your documents might look like  
> and what the search results might look like. That'd make it easier to  
> make suggestions about how to design a solution
> 
> clint
> 
> > I have a unique problem to solve, need your suggestion to solve that.
> > 
> > Index Requirement :
> > 
> > 1. i need to index response with multiple titles with one unique  
> > response id.
> > 
> > Search Requirement:
> > 
> > 1. i need to search some response title text and need to return say 30  
> > or some defined count of response id with the most matched response  
> > title
> > 2. return will only have response Id and most matched title.
> > 
> > Please suggest how should i index and create a query for the same.
> > 
> > --  
> > You received this message because you are subscribed to the Google  
> > Groups "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send  
> > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 25, 2013, 1:17pm UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877/4 "2013-02-25T13:17:41Z")

</div>

Hiya

> if i search Hello i need output
> 
> [  
> {response\_id:"1001", response: "response title 2 hello"},  
> {response\_id : "1002", "Hello response title 1 Hello hello"} \*\* Return  
> only one title here with the most matched title only

There isn't a clear general solution for this. When you search, it  
returns the matching document, rather than just the parts of the  
document that matched.

That said, in this case, you can use highlighting to return the best  
matching title.

# Create the index - Note, we use the position\_offset\_gap to separate

# the individual titles by 1000 words, otherwise

# ["title one","title two"] would be indexed as "title one title two"

curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
{  
"mappings" : {  
"test" : {  
"properties" : {  
"response\_title" : {  
"position\_offset\_gap" : 1000,  
"type" : "string"  
}  
}  
}  
}  
}  
'

# Index some data:

curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"response\_id" : "1001",  
"response\_title" : [  
"response title 1",  
" response title 2 hello "  
]  
}  
'  
curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"response\_id" : "1002",  
"response\_title" : [  
"Hello response title 1 Hello hello",  
" response title 2 hello "  
]  
}  
'  
curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"response\_id" : "1003",  
"response\_title" : [  
"response title 1",  
" response title 2"  
]  
}  
'

# Search the data with highlighting, and number\_of\_fragments = 1

# and fragment\_size = 1000 (so that there is no overlap between titles)

curl -XGET '[http://127.0.0.1:9200/test/test/\_search?pretty=1](http://127.0.0.1:9200/test/test/_search?pretty=1)' -d '  
{  
"query" : {  
"match" : {  
"response\_title" : "Hello"  
}  
},  
"highlight" : {  
"fragment\_size" : 1000,  
"fields" : {  
"response\_title" : {}  
},  
"post\_tags" : [  
""  
],  
"pre\_tags" : [  
""  
],  
"number\_of\_fragments" : 1  
}  
}  
'

# [Mon Feb 25 14:16:46 2013] Response:

# {

# "hits" : {

# "hits" : [

# {

# "\_source" : {

# "response\_id" : "1002",

# "response\_title" : [

# "Hello response title 1 Hello hello",

# " response title 2 hello "

# ]

# },

# "\_score" : 0.625,

# "\_index" : "test",

# "\_id" : "MFXGNXZXTpm1ZbsHSpD0Yw",

# "\_type" : "test",

# "highlight" : {

# "response\_title" : [

# "Hello response title 1 Hello hello"

# ]

# }

# },

# {

# "\_source" : {

# "response\_id" : "1001",

# "response\_title" : [

# "response title 1",

# " response title 2 hello "

# ]

# },

# "\_score" : 0.11506981,

# "\_index" : "test",

# "\_id" : "1V6H\_lS8TMyxhrKieSxaDQ",

# "\_type" : "test",

# "highlight" : {

# "response\_title" : [

# " response title 2 hello "

# ]

# }

# }

# ],

# "max\_score" : 0.625,

# "total" : 2

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "took" : 6

# }

clint

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:49am UTC](https://discuss.elastic.co/t/please-suggest-how-to-achieve-following-with-elasticsearch/10877/5 "2017-07-06T02:49:43Z")

</div>


