How to do a elasticsearch query search from NodeJS using wildcard and space


(Akila Mendis) #1

I have set of products indexed in elasticsearch. I`m searching for "title" on my schema. When I search "fre" or "fresh" I see a result. But when I search for "small fresh" I don't see any result. Is it possible to use wildcard with spaces ? I added es_indexed: "not_analyzed" but no luck.

This is my Product Schema

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var mongoosastic = require("mongoosastic");
const deepPopulate = require("mongoose-deep-populate")(mongoose);
var Owner = require("./user");
var Category = require("./category");
var Reviews = require("./review");

const ProductSchema = new Schema(
  {
    category: {
      type: Schema.Types.ObjectId,
      ref: "Category",
      es_indexed: true,
      es_type: "nested",
      es_include_in_parent: true
    },
    owner: {
      type: Schema.Types.ObjectId,
      ref: "User",
      es_indexed: true,
      es_type: "nested",
      es_include_in_parent: true
    },
    reviews: [
      {
        type: Schema.Types.ObjectId,
        ref: "Review",
        es_indexed: true,
        es_type: "nested",
        es_include_in_parent: true
      }
    ],
    image: { type: String, es_indexed: true },
    title: {
      type: String,
      es_indexed: "not_analyzed"
    },
    description: { type: String, es_indexed: true },
    price: { type: Number, es_indexed: true },
    crated: { type: Date, default: Date.now, es_indexed: true }
  },
  {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
  }
);

ProductSchema.virtual("averageRating").get(function() {
  var rating = 0;
  if (this.reviews.length == 0) {
    rating = 0;
  } else {
    this.reviews.map(review => {
      rating += review.rating;
    });
    rating = rating / this.reviews.length;
  }

  return rating;
});

ProductSchema.plugin(deepPopulate);
ProductSchema.plugin(mongoosastic, {
  populate: [{ path: "category" }, { path: "owner" }, { path: "reviews" }]
});


let Model = mongoose.model("Product", ProductSchema);
Model.createMapping(function(err, mapping) {
  if (err) {
    console.log("error creating mapping (you can safely ignore this)");
    console.log(err);
  } else {
    console.log("mapping created!");
    console.log(mapping);
  }
});
var stream = Model.synchronize();
var count = 0;

stream.on("data", (err, doc) => {
  console.log(doc);
  count++;
});
stream.on("close", () => console.log("indexed " + count + " documents!"));
stream.on("error", err => console.log(err));
Model.SyncToAlgolia();
Model.SetAlgoliaSettings({
  searchableAttributes: ["title"]
});
module.exports = Model;

This is my Search function

async function search(frompage) {
  let fullString = "*" + "small fresh" + "*";
  let startsFrom = frompage * 10;
  console.log(fullString);
  const response = await esClient.search({
    index: "products",
    type: "product",
    from: startsFrom,
    body: {
      query: {
        wildcard: {
          title: fullString
        }
      }
    }
  });
  return response;
}

(David Pilato) #2

It depends on how things are indexed basically.

It'd be bette to provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce in Kibana Console and if needed fix your problem.

Anyway, I'd definitely not use wildcard specifically with something like *foo*. As written in documentation:

Note that this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow wildcard queries, a wildcard term should not start with one of the wildcards * or ?.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.