coder
coder

Reputation: 538

how to create the index from mongoose in elastic search in node,expressjs

I want to create the index in elastic search with mongoose, express but there is no documentation available. I try mongoosastic but that is not comfortable.

So can anybody help me?

Upvotes: 0

Views: 850

Answers (3)

coder
coder

Reputation: 538

i have find a solution for misspelled searching and full text based near by searching.first i will define the fuzzy search for misspelling word.

 var searchParams = {
      index: 'product',
      body: {
      "query": {
        "query_string": {
//search keyword is string which is searched
          "query": searchKeyword+'~',

        }
      }
    }
    };
esClient.search(searchParams,function(err, response){
})

another searching for more like this ...............

    "more_like_this": {
           "fields": [
             "productName","description","brand"
           ],
//searchKeyword is a string variable for searching
           "like_text": searchKeyword,
           "min_term_freq": 1,
             "min_doc_freq": 1,
           "max_query_terms": 12
         }
      }
    }

if you want to fuzzy result then also try this

var searchParams = {
  index: 'product',
  body: {
      query:{
      "fuzzy" : {
    "ProductName" : {
        "value" :         searchKeyword,
        "boost" :         1.0,
        "fuzziness" :     3,
        "prefix_length" : 0,
        "max_expansions": 100
    }
}
  }
    }
    }

Upvotes: 0

coder
coder

Reputation: 538

this is working fine for indexing.so i have add the search query.my search query is like this

 esClient.search({
            'index': 'product',
            //'q': '*'+searchKeyword+'*' this is searching query
            'body': {
                'query': {
                    'prefix': {'_all': searchKeyword}
                }
            }
        },function(err, response){ 
})

But there is a problem .its searches only text based search not misspelled,auto correction,nearby,similar etc.i want to search query who is checked misspelled,similar,nearby all results.so plzz anbody help me. thanks.

Upvotes: 0

Mykola Borysyuk
Mykola Borysyuk

Reputation: 3411

You can use this module

https://github.com/elastic/elasticsearch-js

It's pretty simple to use and have a lot of documentation.

Just connect to DB-> get records that you need-> for each record run publish(client.bulk method).

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

EDIT Here is the example

var es = require('elasticsearch'); var client = new es.Client({ host: 'localhost:9200', log: 'error' }); //doc is the mongoDB mocument var bulkData = [{index: {_index: "yourIndexName", _type: "Any type", _id: doc._id}}, doc]; client.bulk({ requestTimeout: 300000, body: bulkData }, function(err, response){//final callback here});

Hope this helps.

Upvotes: 2

Related Questions