1

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 Answers2

1

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"
                }
            }
        ]
Amit
  • 30,756
  • 6
  • 57
  • 88
  • Thank you for the response, but I cannot use query_string, as my search is sensitive to the terms ordering. So, have to use the phrase query, but looks like phrase query doesn't support the conditional operator. Unfortunately don't see any direct reference to this in elastic search documentation. – user2730259 Feb 15 '21 at 16:26
  • 1
    Yes, for people who are looking for a solution of phrase query limitation your answer will be helpful, appreciate your effort in answering the question. Upvoting your answer – user2730259 Feb 22 '21 at 04:13
0

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.

Amit
  • 30,756
  • 6
  • 57
  • 88
  • Thank you, but my question is related to OR query in phrase. Your query doesn't have OR. Without OR a single term search works in phrase, but multiple terms search with OR (having hyphen) doesn't works – user2730259 Feb 14 '21 at 07:14
  • @user2730259 ok, added another answer, please have a look – Amit Feb 14 '21 at 08:46