# Simple query on two fields with preference to one of them

**URL:** <https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235>\
**Category:** Elasticsearch\
**Created:** [December 3, 2018, 7:45pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235 "2018-12-03T19:45:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Illidan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/illidan/32/54264_2.png) [@Illidan](https://discuss.elastic.co/u/Illidan)\
**Post date:** [December 3, 2018, 7:45pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/1 "2018-12-03T19:45:15Z")

</div>

I have a document with two fields; "title" and "description".

When performing query, I want to give a significant preference to documents having search term in their "title", e.g. show all documents with term in "title" and only after then show documents with term in "description". In other words, documents with term in "title" shall score much much higher than documents with term in "description".

I using SimpleQueryString since I allow to provide expressions as search terms.

I was trying to provide different boost values to "title" and "description" fields (e.g. 100 vs 1) and I was trying to use DisMax with these Boost values for each of the queries - it doesn't seems to work. It looks like boost values simply ignored.

Is there any way to achieve what I looking for with SimpleQueryString?

I using ES 6.4 with NEST (C#)

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [December 7, 2018, 3:32pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/2 "2018-12-07T15:32:02Z")

</div>

your approach seems sane. Can you post the queries you are sending. Preferably the json string that get's send to elasticsearch.

---

<div class="post-metadata">

**Author:** ![Illidan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/illidan/32/54264_2.png) [@Illidan](https://discuss.elastic.co/u/Illidan)\
**Post date:** [December 8, 2018, 8:43am UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/3 "2018-12-08T08:43:24Z")

</div>

Hi Simon,

Here is what I do (NEST, unfortunately I don't have it as JSON):

string criteria = "KuKu";

Func\<QueryContainerDescriptor, QueryContainer\> func = q =\>  
{  
QueryContainer query = q.SimpleQueryString(qs =\> qs.Query(criteria)  
.Fields(f =\> f  
.Field(fi =\> fi.Description)  
.Field(fi=\> fi.Title, 100)  
.DefaultOperator(Operator.And));

return query ;  
}

I don't see that received search results favor in some way "Title". Instead, many of first results (e.g. results with highest score) are based on "Description" field. I see that results with "KuKu" in "Description" appear before results with "KuKu" in "Title".

I would expect that **all** results with "KuKu" in "Title" will appear before any of results with "KuKu" in "Description", since I gave "Title" much higher Boost value.

Is there anything I doing wrong?

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [December 9, 2018, 9:38pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/4 "2018-12-09T21:38:47Z")

</div>

I suspect that this is an effect of document frequency. For instance if you have a `firstname` and `surname` field and you have a doc like this`{ "firstname" : "Paul", "surname" : "Simon"}` in the index you will very likely see it some up if you search for `simon` on both fields. The reason is that `simon` is a rare term in the `surname` field but not in the `firstname`. you can try to prevent this by changing the `type` to `type: cross_fields` see [this](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields) for details. If that doesn't help, I think you should provide a response from the [explain](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-explain.html) API

---

<div class="post-metadata">

**Author:** ![Illidan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/illidan/32/54264_2.png) [@Illidan](https://discuss.elastic.co/u/Illidan)\
**Post date:** [December 12, 2018, 7:45pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/5 "2018-12-12T19:45:11Z")

</div>

Indeed it looks much better now, using MultiMatch with CrossFields.

However, in order to be able to use CrossFields, I had to give up on SimpleQueryString. This is very unfortunate, since it provides very important for me ability to parse query expressions.

Is there any way to keep them both?

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [December 12, 2018, 10:18pm UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/6 "2018-12-12T22:18:59Z")

</div>

you can set `type` on SimpleQueryString as well. it should have the same effect. Another way is to use `copy_to` in your mapping and copy both fields in an uber field and search just in that one field for scoring. That is common practice.

---

<div class="post-metadata">

**Author:** ![Illidan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/illidan/32/54264_2.png) [@Illidan](https://discuss.elastic.co/u/Illidan)\
**Post date:** [December 13, 2018, 5:33am UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/7 "2018-12-13T05:33:44Z")

</div>

I can't find anywhere in SimpleQueryString's api anything related to type (I using NEST). Can you please point me to the relevant method?

Thank you for your help

---

<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:** [January 10, 2019, 5:47am UTC](https://discuss.elastic.co/t/simple-query-on-two-fields-with-preference-to-one-of-them/159235/8 "2019-01-10T05:47:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
