Tuesday, 21 July 2015

Search like a Google with Elasticsearch. Autocomplete, Did you mean and search for items.


Right now i am working with Elasticsearch for more than one year. In this post i would like to share my knowledge how to build full text search on documents, make user spell corrections and suggest user what to type depends on your index. To work with elasticsearch i preffer to use head plugin instead of curl librarry, its easy and i have some extra tool such as browse index documents, build query and so on.

Create and Configure Index

To begin with index configuration open http://localhost:9200/_plugin/head/ (PS. You should have head plugin installed) then go to any request tab and make request to create index
PUT http://localhost:9200/your_index_name/
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "stemmer": {
            "type": "stemmer",
            "language": "english"
          },
          "autocompleteFilter": {
            "max_shingle_size": "5",
            "min_shingle_size": "2",
            "type": "shingle"
          },
          "stopwords": {
            "type": "stop",
            "stopwords": [
              "_english_"
            ]
          }
        },
        "analyzer": {
          "didYouMean": {
            "filter": [
              "lowercase"
            ],
            "char_filter": [
              "html_strip"
            ],
            "type": "custom",
            "tokenizer": "standard"
          },
          "autocomplete": {
            "filter": [
              "lowercase",
              "autocompleteFilter"
            ],
            "char_filter": [
              "html_strip"
            ],
            "type": "custom",
            "tokenizer": "standard"
          },
          "default": {
            "filter": [
              "lowercase",
              "stopwords",
              "stemmer"
            ],
            "char_filter": [
              "html_strip"
            ],
            "type": "custom",
            "tokenizer": "standard"
          }
        }
      }
    }
  }
}
Now let me explain what we did.
  • default  - This analyzer will be used to search documents in our storage. Analyzer is using following filter
    • html_strip - this filter will act first it will remove html in text if preset or decode html characters, is very handy if you using non English language then some characters are encoded
    • lowercase - since elasticsearch divide text to terms they will be case sensitive so that is why text must be lower-cased so it will not depend on user input and will work with any text
    • stopwords - filter which will remove stop words, like is, a, all, an, etc. We don't want this common used words to score our documents since they occur very often but should have no impact on our search
    • stemmer - will finish cleaning text and token. 
  • didYouMean - this analyzer is simple since most of tasks will be done by elasticsearch native query. But again it will use lowercase filter, html_strip.
  • autocomplete - this anlyzer will be used to suggest user what to type depends on his input  and index content. For instance if user type ad,  ES should suggest what else user can type depends on commonly used text phrases in index, so it could be address, addon depends what was commonly used in index. This analyzer is using:
    • shingle filter his role will be take text and split to blocks like "this is example", "is example", "example". Later i will show how this analyzer works in real life.
We can test each analyzer to see how elasticsearch will save tokens using analyze api. To test autocomplete simply run
http://localhost:9200/your_index_name/_analyze?pretty&analyzer=autocomplete&text=Recommend%20questions%20get%20too%20fulfilled.%20He%20fact%20in%20we%20case%20miss%20sake.%20Entrance%20be%20throwing%20he%20do%20blessing%20up.
in your browser. 
{
  "tokens" : [ {
    "token" : "recommend",
    "start_offset" : 0,
    "end_offset" : 9,
    "type" : "<ALPHANUM>",
    "position" : 1
  }, {
    "token" : "recommend questions",
    "start_offset" : 0,
    "end_offset" : 19,
    "type" : "shingle",
    "position" : 1
  }, {
    "token" : "recommend questions get",
    "start_offset" : 0,
    "end_offset" : 23,
    "type" : "shingle",
    "position" : 1
  }, {
    "token" : "recommend questions get too",
    "start_offset" : 0,
    "end_offset" : 27,
    "type" : "shingle",
    "position" : 1
  }, {
    "token" : "recommend questions get too fulfilled",
    "start_offset" : 0,
    "end_offset" : 37,
    "type" : "shingle",
    "position" : 1
  }, {
    "token" : "questions",
    "start_offset" : 10,
    "end_offset" : 19,
    "type" : "<ALPHANUM>",
    "position" : 2
  }, {
    "token" : "questions get",
    "start_offset" : 10,
    "end_offset" : 23,
    "type" : "shingle",
    "position" : 2
  }, {
    "token" : "questions get too",
    "start_offset" : 10,
    "end_offset" : 27,
    "type" : "shingle",
    "position" : 2
  }, {
    "token" : "questions get too fulfilled",
    "start_offset" : 10,
    "end_offset" : 37,
    "type" : "shingle",
    "position" : 2
  }, {
    "token" : "get",
    "start_offset" : 20,
    "end_offset" : 23,
    "type" : "<ALPHANUM>",
    "position" : 3
  }, {
    "token" : "get too",
    "start_offset" : 20,
    "end_offset" : 27,
    "type" : "shingle",
    "position" : 3
  }, {
    "token" : "get too fulfilled",
    "start_offset" : 20,
    "end_offset" : 37,
    "type" : "shingle",
    "position" : 3
  }, {
    "token" : "too",
    "start_offset" : 24,
    "end_offset" : 27,
    "type" : "<ALPHANUM>",
    "position" : 4
  }, {
    "token" : "too fulfilled",
    "start_offset" : 24,
    "end_offset" : 37,
    "type" : "shingle",
    "position" : 4
  }, {
    "token" : "fulfilled",
    "start_offset" : 28,
    "end_offset" : 37,
    "type" : "<ALPHANUM>",
    "position" : 5
  } ]
}

Mapping

PUT http://localhost:9200/your_index_name/_mapping/YOUR_TYPE_NAME/
{
  "YOUR_TYPE_NAME": {
    "properties": {
      "autocomplete": {
        "type": "string",
        "analyzer": "autocomplete"
      },
      "description": {
        "type": "string",
        "copy_to": [
          "did_you_mean",
          "autocomplete"
        ]
      },
      "did_you_mean": {
        "type": "string",
        "analyzer": "didYouMean"
      },
      "title": {
        "type": "string",
        "copy_to": [
          "autocomplete",
          "did_you_mean"
        ]
      }
    }
  }
}
In my solution i am using i have only title and description, and then they are copied to autocomplete and did_you_mean fields.
In my original application i don't have mapping at all because i don't have any knowledge  which fields customer has and i have dynamic mapping, where all my string typed fields are copied to medata field, such as did_you_mean or autocomplete. It is also possible use multi_field type, but issue is that for suggest queries there is limitation for one field only.

Search documents and use spell corrections

POST http://localhost:9200/your_index_name/your_type/
{
  "title": "Real sold my in call.",
  "description": "So by colonel hearted ferrars. Draw from upon here gone add one. He in sportsman household otherwise it perceived instantly. Is inquiry no he several excited am. Called though excuse length ye needed it he having. Whatever throwing we on resolved entrance together graceful. Mrs assured add private married removed believe did she. \r\n\r\nIs he staying arrival address earnest. To preference considered it themselves inquietude collecting estimating. View park for why gay knew face. Next than near to four so hand. Times so do he downs me would. Witty abode party her found quiet law. They door four bed fail now have. \r\n\r\nUnpacked reserved sir offering bed judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery. Stairs ladies friend by in mutual an no. Mr hence chief he cause. Whole no doors on hoped. Mile tell if help they ye full name. "
}
POST http://localhost:9200/your_index_name/your_type/
{
  "title": "Use securing confined his shutters.",
  "description": "Delightful as he it acceptance an solicitude discretion reasonably. Carriage we husbands advanced an perceive greatest. Totally dearest expense on demesne ye he. Curiosity excellent commanded in me. Unpleasing impression themselves to at assistance acceptance my or. On consider laughter civility offended oh. \r\n\r\nAdd you viewing ten equally believe put. Separate families my on drawings do oh offended strictly elegance. Perceive jointure be mistress by jennings properly. An admiration at he discovered difficulty continuing. We in building removing possible suitable friendly on. Nay middleton him admitting consulted and behaviour son household. Recurred advanced he oh together entrance speedily suitable. Ready tried gay state fat could boy its among shall. "
}
POST http://localhost:9200/your_index_name/your_type/
{
  "title": "Sense child do state to defer mr of forty.",
  "description": " Become latter but nor abroad wisdom waited. Was delivered gentleman acuteness but daughters. In as of whole as match asked. Pleasure exertion put add entrance distance drawings. In equally matters showing greatly it as. Want name any wise are able park when. Saw vicinity judgment remember finished men throwing. \r\n\r\nFull he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high. "
}
POST http://localhost:9200/your_index_name/your_type/
{
  "title": "His having within saw become ask passed misery giving. ",
  "description": "Recommend questions get too fulfilled. He fact in we case miss sake. Entrance be throwing he do blessing up. Hearts warmth in genius do garden advice mr it garret. Collected preserved are middleton dependent residence but him how. Handsome weddings yet mrs you has carriage packages. Preferred joy agreement put continual elsewhere delivered now. Mrs exercise felicity had men speaking met. Rich deal mrs part led pure will but. \r\n\r\nNeeded feebly dining oh talked wisdom oppose at. Applauded use attempted strangers now are middleton concluded had. It is tried no added purse shall no on truth. Pleased anxious or as in by viewing forbade minutes prevent. Too leave had those get being led weeks blind. Had men rose from down lady able. Its son him ferrars proceed six parlors. Her say projection age announcing decisively men. Few gay sir those green men timed downs widow chief. Prevailed remainder may propriety can and. "
}

Query to search documents and suggest user what to search

POST http://localhost:9200/search_like_a_google/_search/
{
  "suggest": {
    "didYouMean": {
      "text": "test",
      "phrase": {
        "field": "did_you_mean"
      }
    }
  },
  "query": {
    "multi_match": {
      "query": "test",
      "fields": [
        "description",
        "title"
      ]
    }
  }
}
In index there is no any work test so elasticsearch should suggest something else to search and as expected in response there is suggestion from index
{
  • "took"4,
  • "timed_out"false,
  • "_shards": {
    • "total"5,
    • "successful"5,
    • "failed"0
    },
  • "hits": {
    • "total"0,
    • "max_score"null,
    • "hits": [ ]
    },
  • "suggest": {
    • "didYouMean": [
      • {
        • "text""test",
        • "offset"0,
        • "length"4,
        • "options": [
          • {
            • "text""tell",
            • "score"0.08653918
            }
          ]
        }
      ]
    }
}

Query autocomplete

Autocomplete in this solution is more suggestion for user what he can search depends on index content. Idea is that elasticsearch should "take" phrases which are in index and suggest user depends what he wrote as an input.
POST http://localhost:9200/YOUR_INDEX_NAME/_search/
{
  "size": 0,
  "aggs": {
    "autocomplete": {
      "terms": {
        "field": "autocomplete",
        "order": {
          "_count": "desc"
        },
        "include": {
          "pattern": "c.*"
        }
      }
    }
  },
  "query": {
    "prefix": {
      "autocomplete": {
        "value": "c"
      }
    }
  }
}
This query works in following way. Query search for all terms which starts from "c" then using aggregation terms it will calculate usage of terms. For auto-complete field shingle term tokenizer is used which will divide text to kind of groups and depends on their usage elasticsearch will suggest user what to type.
In response there is suggestions depends on index.
{
  • "took"3,
  • "timed_out"false,
  • "_shards": {
    • "total"5,
    • "successful"5,
    • "failed"0
    },
  • "hits": {
    • "total"4,
    • "max_score"0,
    • "hits": [ ]
    },
  • "aggregations": {
    • "autocomplete": {
      • "doc_count_error_upper_bound"1,
      • "sum_other_doc_count"112,
      • "buckets": [
        • {
          • "key""carriage",
          • "doc_count"2
          }
        • ,
        • {
          • "key""chief",
          • "doc_count"2
          }
        • ,
        • {
          • "key""concluded",
          • "doc_count"2
          }
        • ,
        • {
          • "key""call",
          • "doc_count"1
          }
        • ,
        • {
          • "key""called",
          • "doc_count"1
          }
        • ,
        • {
          • "key""called though",
          • "doc_count"1
          }
        • ,
        • {
          • "key""called though excuse",
          • "doc_count"1
          }
        • ,
        • {
          • "key""called though excuse length",
          • "doc_count"1
          }
        • ,
        • {
          • "key""called though excuse length ye",
          • "doc_count"1
          }
        • ,
        • {
          • "key""can",
          • "doc_count"1
          }
        ]
      }
    }
}
This solution is useful because if index grow then ES will provide smarter suggestion. And most important that search requests will return some documents.
In next post i will show how to do the same using Asp.Net MVC, Nest and angular for autocomplete.
Idea how to implement autocomplete i took from this blog
Volodymyr Bilyachat Web Developer