# Query building question

**URL:** <https://discuss.elastic.co/t/query-building-question/7927>\
**Category:** Elasticsearch\
**Created:** [May 31, 2012, 12:47pm UTC](https://discuss.elastic.co/t/query-building-question/7927 "2012-05-31T12:47:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maik](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@maik](https://discuss.elastic.co/u/maik)\
**Post date:** [May 31, 2012, 12:47pm UTC](https://discuss.elastic.co/t/query-building-question/7927/1 "2012-05-31T12:47:22Z")

</div>

Hi together,

I'm using elasticsearch quite for a while, but I'm not sure if the queries  
I build are ok.

I'm indexing products of an ecommerce webshop with only a few fields: name,  
manufacturername, code, referencecode.

The visitors of the shop can use the global searchfield to initialize a  
search.  
Let's say one choose "usb stick" for the search string. My current query is  
build as followed:

{

"query": {

"bool": {

"should": [

{

"fuzzy\_like\_this\_field": {

"name": {

"like\_text": "usb stick",

"boost": 10.0,

"min\_similarity": 0.7

}

}

},

{

"fuzzy\_like\_this\_field": {

"manufacturer": {

"like\_text": "usb stick",

"boost": 5.0,

"min\_similarity": 0.7

}

}

},

{

"fuzzy\_like\_this\_field": {

"code": {

"like\_text": "usb stick",

"boost": 1.0,

"min\_similarity": 0.7

}

}

},

{

"fuzzy\_like\_this\_field": {

"refcode": {

"like\_text": "usb stick",

"boost": 10,

"min\_similarity": 0.7

}

}

}

]

}

}

}

I'm using the field based fuzzy queries to place a "boost" per field.  
With it, if I'm right, I can for example specify "the name field is more  
important than the code field".

The results are ok. Nothing to grumble about. But this request tooks  
300-500ms to execute.

So my final question: Is it possible to rewrite the query, so it has the  
same effect (field priority + fuzzy), but faster?

Index size: 50k documents  
2 shards, 1 replica each  
2 servers, more than enough memory / cpu.

Thank you in advance  
Greetings  
Maik

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 3, 2012, 9:24am UTC](https://discuss.elastic.co/t/query-building-question/7927/2 "2012-06-03T09:24:50Z")

</div>

Fuzzy queries are sadly slow... . One option to work around that is for  
example to use ngrams to analyze the fields during indexing time. I would  
use multi field mapping, one with regular analyzer, and one with ngrams,  
and then do the search on the field with the regular analyzer with a high  
boost, and one on the ngram based one with a lower boost.

On Thu, May 31, 2012 at 2:47 PM, maik [maik2102@googlemail.com](mailto:maik2102@googlemail.com) wrote:

> Hi together,
> 
> I'm using elasticsearch quite for a while, but I'm not sure if the queries  
> I build are ok.
> 
> I'm indexing products of an ecommerce webshop with only a few fields:  
> name, manufacturername, code, referencecode.
> 
> The visitors of the shop can use the global searchfield to initialize a  
> search.  
> Let's say one choose "usb stick" for the search string. My current query  
> is build as followed:
> 
> {
> 
> "query": {
> 
> "bool": {
> 
> "should": [
> 
> {
> 
> "fuzzy\_like\_this\_field": {
> 
> "name": {
> 
> "like\_text": "usb stick",
> 
> "boost": 10.0,
> 
> "min\_similarity": 0.7
> 
> }
> 
> }
> 
> },
> 
> {
> 
> "fuzzy\_like\_this\_field": {
> 
> "manufacturer": {
> 
> "like\_text": "usb stick",
> 
> "boost": 5.0,
> 
> "min\_similarity": 0.7
> 
> }
> 
> }
> 
> },
> 
> {
> 
> "fuzzy\_like\_this\_field": {
> 
> "code": {
> 
> "like\_text": "usb stick",
> 
> "boost": 1.0,
> 
> "min\_similarity": 0.7
> 
> }
> 
> }
> 
> },
> 
> {
> 
> "fuzzy\_like\_this\_field": {
> 
> "refcode": {
> 
> "like\_text": "usb stick",
> 
> "boost": 10,
> 
> "min\_similarity": 0.7
> 
> }
> 
> }
> 
> }
> 
> ]
> 
> }
> 
> }
> 
> }
> 
> I'm using the field based fuzzy queries to place a "boost" per field.  
> With it, if I'm right, I can for example specify "the name field is more  
> important than the code field".
> 
> The results are ok. Nothing to grumble about. But this request tooks  
> 300-500ms to execute.
> 
> So my final question: Is it possible to rewrite the query, so it has the  
> same effect (field priority + fuzzy), but faster?
> 
> Index size: 50k documents  
> 2 shards, 1 replica each  
> 2 servers, more than enough memory / cpu.
> 
> Thank you in advance  
> Greetings  
> Maik

---

<div class="post-metadata">

**Author:** ![maik](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@maik](https://discuss.elastic.co/u/maik)\
**Post date:** [June 4, 2012, 10:27am UTC](https://discuss.elastic.co/t/query-building-question/7927/3 "2012-06-04T10:27:24Z")

</div>

Thx Shay,

I'll give it a try.

Greetings  
maik

---

<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:25am UTC](https://discuss.elastic.co/t/query-building-question/7927/4 "2017-07-06T03:25:45Z")

</div>


