Elastic Search 1.4 phrase query with OR operator with hyphen (-) in search string

420 Views Asked by At

I have a issue in Elastic search 1.4 phrase query. I am creating a below index with the data.

curl -XPUT localhost:9200/test

curl -XPOST localhost:9200/test/doc/1 -d '{"field1" : "abc-xyz"}'

curl -XPOST localhost:9200/test/doc/2 -d '{"field1" : "bcd-gyz"}'

So by default field1 is analyzed by elastic search with default analyzer.

I am searching below phrase query but its not returning any result.

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "should": [
            {
              "query": {
                "multi_match": {
                  "query": "abc\\-xyz OR bcd\\-gyz",
                  "type": "phrase",
                  "fields": [
                    "field1"
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
}

So elastic search phrase query is not working with OR operator. Any idea why its not working, is it a limitation of elastic search because of special character hyphen (-) in text?

2

There are 2 best solutions below

2
On

Below query works fine for me

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "should": [
                        {
                            "query": {
                                "multi_match": {
                                    "query": "abc-xyz", // note passing only one query without escaping hyphen 
                                    "type": "phrase",
                                    "fields": [
                                        "title"
                                    ]
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

Search results with explain param

 "hits": [
            {
                "_shard": 3,
                "_node": "1h3iipehS2abfclj51Vtsg",
                "_index": "phrasemulti",
                "_type": "doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "abc-xyz"
                },
                "_explanation": {
                    "value": 1.0,
                    "description": "ConstantScore(BooleanFilter(QueryWrapperFilter(title:\"abc xyz\"))), product of:",
                    "details": [
                        {
                            "value": 1.0,
                            "description": "boost"
                        },
                        {
                            "value": 1.0,
                            "description": "queryNorm"
                        }
                    ]
                }
            }
        ]

Verified its returning results according to phrase as query abc-xy doesn't return any result.

2
On

Based on the comment, adding a answer using query string which works with OR in phrase with multiple search, it didn't work with multiple multi-match hence have to use query string.

Using the same indexed doc, added in previous answer, but with below search query.

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "\"abc-xyz\" OR \"bcd-gyz\"",
                        "fields": [
                            "title"
                        ]
                    }
                }
            ]
        }
    }
}

Search results

 "hits": [
            {
                "_index": "phrasemulti",
                "_type": "doc",
                "_id": "1",
                "_score": 0.05626005,
                "_source": {
                    "title": "bcd-gyz"
                }
            },
            {
                "_index": "phrasemulti",
                "_type": "doc",
                "_id": "2",
                "_score": 0.05626005,
                "_source": {
                    "title": "abc-xyz"
                }
            }
        ]

When you remove few char, pharse query won't work or when you change operator to AND, sample data doesn't return search results which is expected.

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "\"abc-xyz\" OR \"bcd-gz\"",
                        "fields": [
                            "title"
                        ]
                    }
                }
            ]
        }
    }
}

Returns only one search result, as there is no phrase bcd-gz exist in sample data.

  "hits": [
            {
                "_index": "phrasemulti",
                "_type": "doc",
                "_id": "2",
                "_score": 0.05626005,
                "_source": {
                    "title": "abc-xyz"
                }
            }
        ]