# Combine query string query and nested query

**URL:** <https://discuss.elastic.co/t/combine-query-string-query-and-nested-query/1465>\
**Category:** Elasticsearch\
**Created:** [May 28, 2015, 12:15pm UTC](https://discuss.elastic.co/t/combine-query-string-query-and-nested-query/1465 "2015-05-28T12:15:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frobfoo](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@frobfoo](https://discuss.elastic.co/u/frobfoo)\
**Post date:** [May 28, 2015, 12:15pm UTC](https://discuss.elastic.co/t/combine-query-string-query-and-nested-query/1465/1 "2015-05-28T12:15:42Z")

</div>

Hello,

I have the following (simplified) document/mapping:

```
"mappings" : {
  "hotel" : {
    "properties" : {
      "name" : "string",
      "city" : "string",
      "tags" : {
        "type": "nested",
        "properties" : {
          "name" : {
            "type" : "string"
          },
          "rating" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}

```

I want to provide a search function that supports exact phrase matches and operators like + to force inclusion of certain search terms. So, for example, a search for "new york +spa" should only return results with the term "spa" either in one of the string attributes or in the list of tags.

To make it a bit more interesting, the score of the results should be influenced by the tags' rating.

My naive approach was to use a query like this:

```
"query" : {
  "bool" : {
    "should" : [ {
      "simple_query_string" : {
        "query" : "new york +spa",
        "fields" : ["name", "city"]
      }
    }, {
      "nested" : {
        "query" : {
          "function_score" : {
            "query" : {
              "match" : {
                "name" : {
                  "query" : "new york +spa",
                  "type" : "boolean"
                }
              }
            },
            "functions" : [ {
              "script_score" : {
                "script" : "(doc['rating'].empty ? 1 : doc['rating'].value)"
              }
            } ]
          }
        },
        "path" : "tags"
      }
    } ]
  }
}

```

But, of course, that way the query string search with operators won't be working as expected, because I have split up the queries.

Any ideas on how to solve this?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Jason\_Wee](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jason\_Wee](https://discuss.elastic.co/u/Jason_Wee)\
**Post date:** [May 29, 2015, 1:36pm UTC](https://discuss.elastic.co/t/combine-query-string-query-and-nested-query/1465/2 "2015-05-29T13:36:36Z")

</div>

```
"function_score": {
    "(query|filter)": {},
    "boost": "boost for the whole query",
    "FUNCTION": {},
    "boost_mode":"(multiply|replace|...)"
}

```

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)

should not the AND builder filter pass into function score query? it java, it would be something like

BoolFilterBuilder bfb = new BoolFilterBuilder();  
FunctionScoreQueryBuilder fsqb = new FunctionScoreQueryBuilder(bfb);

you can use bfb to quickly filter the item u want and then in the fsqb, you can start to give the score for the results?

hth

jason

---

<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, 12:10am UTC](https://discuss.elastic.co/t/combine-query-string-query-and-nested-query/1465/3 "2017-07-06T00:10:50Z")

</div>


