Query for multiple terms and their order in one text field


(Junkoshimane) #1

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.

(Christoph) #2

I think for that you will need some sort of Span Query, possibly the Span Near query with in_order set to true.


(Christoph) #3

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.

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
        }
    }
}

(Junkoshimane) #4

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.


(Junkoshimane) #5

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.


(system) #6

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