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

**URL:** <https://discuss.elastic.co/t/how-to-do-a-elasticsearch-query-search-from-nodejs-using-wildcard-and-space/137569>\
**Category:** Elasticsearch\
**Created:** [June 27, 2018, 8:26am UTC](https://discuss.elastic.co/t/how-to-do-a-elasticsearch-query-search-from-nodejs-using-wildcard-and-space/137569 "2018-06-27T08:26:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akila\_Mendis](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Akila\_Mendis](https://discuss.elastic.co/u/Akila_Mendis)\
**Post date:** [June 27, 2018, 8:26am UTC](https://discuss.elastic.co/t/how-to-do-a-elasticsearch-query-search-from-nodejs-using-wildcard-and-space/137569/1 "2018-06-27T08:26:46Z")

</div>

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;
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 27, 2018, 8:45am UTC](https://discuss.elastic.co/t/how-to-do-a-elasticsearch-query-search-from-nodejs-using-wildcard-and-space/137569/2 "2018-06-27T08:45:33Z")

</div>

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](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html):

> 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 ?.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 25, 2018, 8:45am UTC](https://discuss.elastic.co/t/how-to-do-a-elasticsearch-query-search-from-nodejs-using-wildcard-and-space/137569/3 "2018-07-25T08:45:37Z")

</div>

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