0

I'm from MySql background. So I don't know much about elasticsearch and it's working.

Here is my requirements

There will be table of resulted records with sorting option on all the column. There will be filter option from where user will select multiple values for multiple columns (e.g, City should be from City1, City2, City3 and Category should be from Cat2, Cat22, Cat6). There will be also search bar where user will enter some text and full text search will be applied on some fields (i.e, City, Area etc).

enter image description here

This image will give better understanding.

Where I'm facing problem is Full Text Search. I have tried some mapping but every time I have to compromise either on Full Text Search or Terms Search. So I think there is no any way to apply both search on same field. But as I told, I don;t know much about elasticsearch. So if any one have solution, it will be appreciated.

Here is what I have applied currently which makes sorting and Terms Searching enable but Full Text Search is not working.

{
    "mappings":{
        "my_type":{
            "properties":{
                "city":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "category":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "area":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "zip":{
                    "type":"string",
                    "index":"not_analyzed"
                },
                "state":{
                    "type":"string",
                    "index":"not_analyzed"
                }
            }
        }
    }
}
Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60

2 Answers2

0

You can update the mapping with multifields with two mappings one for full text and another for terms search. Here's a sample mapping for city.

{
    "city": {
        "type": "string",
        "index": "not_analyzed",
        "fields": {
           "fulltext": {
               "type": "string"
           }
         }
     }
}

Default mapping is for terms search, so when terms search is required, you could simple query in "city" field. But, you need full-text search, query must be performed on "city.fulltext". Hope this helps.

Nishant
  • 450
  • 2
  • 6
0

Full-text search won't work on not_analyzed fields and sorting won't work on analyzed fields.

You need to use multi-fields.

It is often useful to index the same field in different ways for different purposes. This is the purpose of multi-fields. For instance, a string field could be mapped as a text field for full-text search, and as a keyword field for sorting or aggregations:

For example :

{
  "mappings": {
    "my_type": {
      "properties": {
        "city": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        } ...
      }
    }
  }
}

Use the dot notation to sort by city.raw :

{
  "query": {
    "match": {
      "city": "york" 
    }
  },
  "sort": {
    "city.raw": "asc" 
  }
}
maxbeaudoin
  • 6,546
  • 5
  • 38
  • 53