Ibhagawati
Ibhagawati

Reputation: 129

Perform exact match with Elastic Search text type field with multiple value

Hi I have milions of recods in elastic search where one of my field (textlowercase) is of type "text".

Now I want to search this "text" type field for multiple words how can I do this.

The issue is that since it is a text field it is analyzed and split into tokens. for e.g: in SQL I want something like this

select textlowercase from table where textlowercase like '%abc%' or '%bbc%' or '%my text%'

I have tried "not analyzed" and changing the type to "keyword" it does not help.

I am using Elastic search 7

Here is my mappings:

{
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [

          ],
          "filter": [
            "lowercase"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "fbdata": {
    "mappings": {
      "properties": {
        "createdatutc": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "createdbyname": {
          "type": "keyword"
        },
        "groupname": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        },
        "insertedatutc": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "postid": {
          "type": "keyword"
        },
        "posttype": {
          "type": "keyword"
        },
        "posturl": {
          "type": "keyword"
        },
        "textlowercase": {
          "type": "text",
          "analyzer": "my_analyzer",
          "fielddata": true
        }
      }
    }
  }
}

And here is my query

{
  "index": "fbdata",
  "type": "_doc",
  "body": {
    "from": 0,
    "size": 500000,
    "query": {
      "bool": {
        "should": [ {
          "match": {
            "textlowercase": "*cowmilk*"
          }
        }, {
          "match": {
            "textlowercase": "*Gaay ka doodh*"
          }
        }, {
          "match": {
            "textlowercase": "*cow ka*"
          }
        }, {
          "match": {
            "textlowercase": "*bakri ka*"
          }
        }, {
          "match": {
            "textlowercase": "*goatmilk*"
          }
        }],
        "must": [{
          "range": {
            "createdatutc": {
              "gte": "2019-01-01",
              "lt": "2019-03-31",
              "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
            }
          }
        }]
      }
    }
  }
}

Upvotes: 0

Views: 535

Answers (1)

Vikash Kumar Verma
Vikash Kumar Verma

Reputation: 1078

You can use match_phrase query.

{
  "query": {
    "match_phrase": {
      "FIELD": "PHRASE"
    }
  }
}

See more details https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html

Upvotes: 1

Related Questions