maddy
maddy

Reputation: 5

Elasticsearch match_phrase_prefix query not working with analyzer_startswith

I am new to Elasticsearch. When I use match_phrase_prefix on a single field it works, but if I use match_phrase_prefix on multiple fields it doesn't work.

If I search for hybrid,121 with the query below I get results when field3 length is greater than 4 digits. I need to get result starts with 1st digit.

My query looks like this

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "feild1": "p"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase_prefix": {
                  "feild2": {
                    "query": "HYBRID "
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "feild3": {
                    "query": "121"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "from": "0",
  "size": "10"
}

field3 is long in the database. I convert long to char when I load the data.

My mappings

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "analyzer_startswith": {
            "tokenizer": "keyword",
            "filter": "lowercase"
          }
        }
      }
    }
  },
  "mappings": {
    "pty": {
      "properties": {
        "feild2": {
          "search_analyzer": "analyzer_startswith",
          "analyzer": "analyzer_startswith",
          "type": "string"
        },
        "feild3": {
          "search_analyzer": "analyzer_startswith",
          "analyzer": "analyzer_startswith",
          "type": "string"
        }
      }
    }
  }
}

Upvotes: 0

Views: 3566

Answers (1)

Hatim Stovewala
Hatim Stovewala

Reputation: 1251

For your use case. Your mapping should be like this:

PUT index_name
{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },
    "mappings": {
        "type_name": {
            "properties": {
                "field1": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                },
                "field2": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                },
                "field3": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                }
            }
        }
    }
}

Sample document:

POST index_name/type_name
{
  "field1":"hatim",
  "field2":"stovewala",
  "field3":"india"
}

Query should be like this:

GET index_name/type_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "field1": "hat"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase_prefix": {
                  "field2": "s"
                }
              },
              {
                "match_phrase_prefix": {
                  "field3": "in"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions