1

I am trying to convert my MYSQL query to Elasticsearch. The query includes multiple conditions on different fields. Let me explain what i am trying to achieve. My Mysql query is

Select * from data_fl where city IN 'miami,miamibeach,etc' AND phone!=0 AND (name like '%abc%' OR address like '%abc%' OR zip_code like '%abc%' OR phone Like '%abc')

how this query can be replicated in elasticsearch. My attempt is

$params = [
                              'index'=>'us_data_'.strtolower($state_code),
                              'body'  => [
                                  'query' => [
                                    'bool'=>[
                                        'filter'=>[
                                            'term'=>['city_code'=>$city_name]
                                        ],

                                      'should' => [
                                        'query_string'=>[
                                          'query'=>"*".$service."*",
                                          'fields'=>['name','contact_no','zip_code','city_code'],
                                        ]
                                      ]
                                    ]
                                  ]
                              ]
                    ];  

But this doesn't return anything. I am using Elasticsearch 7.6 and trying to replicate this query with curl on Kibana but the answer is still the same.

Looking forward for help

As requested the mapping of the index is

{


"mapping": {
    "_doc": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "address": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "city_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "contact_no": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "date_added": {
          "type": "date"
        },
        "date_updated": {
          "type": "date"
        },
        "featured": {
          "type": "long"
        },
        "id": {
          "type": "long"
        },
        "location_id": {
          "type": "long"
        },
        "main_cate": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "slug": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "source": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "state_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "status": {
          "type": "long"
        },
        "zip_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

The document which i accept is

 "hits" : {
"total" : {
  "value" : 10000,
  "relation" : "gte"
},
"max_score" : 1.0,
"hits" : [
  {
    "_index" : "us_data_al",
    "_type" : "_doc",
    "_id" : "8kmR1HABkLcaz3xayZOg",
    "_score" : 1.0,
    "_source" : {
      "promotion" : null,
      "image" : null,
      "name" : "Port City Realty",
      "city_code" : "Mobile",
      "services" : null,
      "promotion_exp_date" : null,
      "tuesdayopen" : null,
      "tuesdayclose" : null,
      "wednesdayopen" : null,
      "thursdayclose" : null,
      "@timestamp" : "2020-03-13T15:44:45.330Z",
      "date_updated" : "2020-03-06T00:00:00.000Z",
      "mondayopen" : null,
      "contact_no" : "2516891228",
      "id" : 1941,
      "fridayclose" : null,
      "featured" : 0,
      "main_cate" : 1,
      "wednesdayclose" : null,
      "sundayopen" : null,
      "state_code" : "AL",
      "video" : null,
      "address" : "4826 Whispering Oaks Lane",
      "user_id" : null,
      "slug" : "2516891228-port-city-realty-mobile-al-36695",
      "timezone" : null,
      "source" : "USA Business",
      "description" : null,
      "fridayopen" : null,
      "price" : null,
      "saturdayopen" : null,
      "saturdayclose" : null,
      "date_added" : "2020-03-05T19:00:00.000Z",
      "thursdayopen" : null,
      "@version" : "1",
      "status" : 1,
      "mondayclose" : null,
      "zip_code" : "36695",
      "private_contact" : null,
      "location_id" : 0,
      "sundayclose" : null
    }
  }
9
  • provide your index mapping and search query in JSON format and some sample documents Commented Mar 20, 2020 at 10:03
  • @es-enthu i have updated the mapping. It is done via Kibana Commented Mar 20, 2020 at 10:08
  • @kool_Coder, could you please provide documents and your expected documents and wouldn't suggest using wildcard query in Elasticsearch and hope you are using ES 7.X ? Commented Mar 20, 2020 at 10:11
  • @OpsterElasticsearchNinja Yes i am using ES7.6 I want user to enter free text which can match business name or address or phone number or zip code in USA. What do you suggest if i dont use WildCards?? Commented Mar 20, 2020 at 10:18
  • @kool_Coder, let me have a look and write query for you Commented Mar 20, 2020 at 10:20

1 Answer 1

2

You are complicating the things and trying to fit MySQL concept in Elasticsearch, In this case, you need to properly define your index mapping(fields data types and their analyzer based on the search requirements) and accordingly build your queries.

I've taken your sample and didn't change your index mapping and sample document, but changed the search query to show, how with your existing data and requirement(may not work in all cases, but you gets an idea) it can bring the search.

Search query

{
    "query": {
        "multi_match": { --> note and read about multi_match query
            "query": "36695",
            "fields": [
                "address",
                "city_code", --> add more fields if you need to be
                "zip_code",
                "contact_no"
            ]
        }
    }
}

Search result brings your sample doc:

 "hits": [
            {
                "_index": "so_mysql_dsl",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "promotion": null,
                    "image": null,
                    "name": "Port City Realty",
                    "city_code": "Mobile",
                    "services": null,
                    "promotion_exp_date": null,
                    "tuesdayopen": null,
                    "tuesdayclose": null,
                    "wednesdayopen": null,
                    "thursdayclose": null,
                    "@timestamp": "2020-03-13T15:44:45.330Z",
                    "date_updated": "2020-03-06T00:00:00.000Z",
                    "mondayopen": null,
                    "contact_no": "2516891228",
                    "id": 1941,
                    "fridayclose": null,
                    "featured": 0,
                    "main_cate": 1,
                    "wednesdayclose": null,
                    "sundayopen": null,
                    "state_code": "AL",
                    "video": null,
                    "address": "4826 Whispering Oaks Lane",
                    "user_id": null,
                    "slug": "2516891228-port-city-realty-mobile-al-36695",
                    "timezone": null,
                    "source": "USA Business",
                    "description": null,
                    "fridayopen": null,
                    "price": null,
                    "saturdayopen": null,
                    "saturdayclose": null,
                    "date_added": "2020-03-05T19:00:00.000Z",
                    "thursdayopen": null,
                    "@version": "1",
                    "status": 1,
                    "mondayclose": null,
                    "zip_code": "36695",
                    "private_contact": null,
                    "location_id": 0,
                    "sundayclose": null
                }
            }
        ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.