Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching whole string with dashes in elasticsearch

I have an elasticsearch query which I am trying to match properly, the field data itself contains -(dashes), the string data are GUIDS

It was not matching properly because it was splitting the term up into separate words split by the -

I have since changed the query to use a match_phrase query like this:

"query": {
    "filtered": {
           "query": {
              "match_phrase":{

                  "guid":{"operator" : "or","query":"bd2acb42-cf01-11e2-ba92-12313916f4be"}
               }
           }
     }
 }

When I am trying to match just one GUIDS, this works just fine.

However I am trying to match multiple GUIDS

So it currently looks like

"query": {
    "filtered": {
           "query": {
              "match_phrase":{

                  "guid":{"operator" : "or","query":"bd2acb42-cf01-11e2-ba92-12313916f4be d1091f08-ceff-11e2-ba92-12313916f4be"}
               }
           }
     }
}

I assume its not working because its trying to match the whole string, and not each GUID separately.

I tried added "analyzer" : "whitespace", to the query, but this broke the query entirely.

So what is the best method to ensure the query is looking for the whole GUID string and allows matching of multiple GUIDS?

like image 637
Lawrence Cooke Avatar asked Oct 04 '22 21:10

Lawrence Cooke


1 Answers

I have been setting the field mapping to not_analyzed for similar purposes.

    "guid" : {
      "type" : "string",
      "index" : "not_analyzed"
    }

Building the query manually then works.

{
    "bool" : {
        "should" : [
            {
                "term" : { "guid" : "bd2acb42-cf01-11e2-ba92-12313916f4be" }
            },
            {
                "term" : { "guid" : "d1091f08-ceff-11e2-ba92-12313916f4be" }
            }
        ],
        "minimum_number_should_match" : 1
    }
}
like image 160
Louis-Philippe Huberdeau Avatar answered Oct 07 '22 23:10

Louis-Philippe Huberdeau