# Query for multiple terms and their order in one text field

**URL:** <https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555>\
**Category:** Elasticsearch\
**Created:** [October 23, 2018, 9:21am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555 "2018-10-23T09:21:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![junkoshimane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junkoshimane/32/36594_2.png) [@junkoshimane](https://discuss.elastic.co/u/junkoshimane)\
**Post date:** [October 23, 2018, 9:21am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/1 "2018-10-23T09:21:44Z")

</div>

Hi all,

I'm new to Elasticsearch. As far as I checked Elasticsearch reference and this site about the following query DSL design, seems like it's impossible but I will be able to realize what I'd like to do using Painless scripting.  
Is my guess correct?

[What I'd like to do with query DSL]

- Multiple terms must exist in one text field.
- And the order of terms must be fixed.

[Example]

- Datatype : Text datatype
- Search terms and their order : "hhh", "eee", "bbb"
- text example No.1 : "aaa bbb ccc ddd eee fff ggg hhh iii jjj"
- text example No.2 : "jjj iii hhh ggg fff eee ddd ccc bbb aaa"
- text example No.3 : "bbb eee hhh iii jjj ggg fff ddd aaa ccc"
- Expected result : Only text example No.2 is hit.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 23, 2018, 10:10am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/2 "2018-10-23T10:10:36Z")

</div>

> [@junkoshimane](#):
>
> Search terms and their order : "hhh", "eee", "bbb"

I think for that you will need some sort of Span Query, possibly the [Span Near](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html) query with `in_order` set to true.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 23, 2018, 10:20am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/3 "2018-10-23T10:20:16Z")

</div>

> [@cbuescher](#):
>
> ... Span Query, possibly the [Span Near](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html) query

I just checked, this should work. You might need to experiment with the "slop" factor a bit, maybe from your application it makes sense that the search terms appear in a certain window.

```auto
PUT /spanexample/_doc/1
{
  "textfield" : "aaa bbb ccc ddd eee fff ggg hhh iii jjj"
}

PUT /spanexample/_doc/2
{
  "textfield" : "jjj iii hhh ggg fff eee ddd ccc bbb aaa"
}

PUT /spanexample/_doc/3
{
  "textfield" : "bbb eee hhh iii jjj ggg fff ddd aaa ccc"
}

POST /spanexample/_search
{
  "query": {
        "span_near" : {
            "clauses" : [
                { "span_term" : { "textfield" : "hhh" } },
                { "span_term" : { "textfield" : "eee" } },
                { "span_term" : { "textfield" : "bbb" } }
            ],
            "slop" : 12,
            "in_order" : true
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![junkoshimane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junkoshimane/32/36594_2.png) [@junkoshimane](https://discuss.elastic.co/u/junkoshimane)\
**Post date:** [October 26, 2018, 1:16am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/4 "2018-10-26T01:16:14Z")

</div>

Hi Christoph,

Thank you for your reply.  
I tested span\_near query in English, it surely realizes what I'd like to do.

But it can't in Japanese in spite of using analyzer for Japanese language.  
I will post other question about it.

---

<div class="post-metadata">

**Author:** ![junkoshimane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/junkoshimane/32/36594_2.png) [@junkoshimane](https://discuss.elastic.co/u/junkoshimane)\
**Post date:** [October 26, 2018, 4:10am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/5 "2018-10-26T04:10:42Z")

</div>

Hi all,

There is a correction.  
span\_near query is possible in Japanese as well.  
When I tested it before, seems like I mistook something.

---

<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:** [November 23, 2018, 4:10am UTC](https://discuss.elastic.co/t/query-for-multiple-terms-and-their-order-in-one-text-field/153555/6 "2018-11-23T04:10:42Z")

</div>

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