Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
55d2881
default mongoose number to long, not double
AxelJunker May 8, 2019
c0afbac
default string to text
AxelJunker May 8, 2019
f7b2156
make generateMapping sync
AxelJunker May 8, 2019
95a12ea
expose generateMapping and getCleanTree
AxelJunker May 8, 2019
eb2f741
don’t forceIndexRefresh by default
AxelJunker May 8, 2019
76f6008
highlight and suggest are not search options
AxelJunker May 8, 2019
4840d0a
expose an error event emitter when bulk indexing
AxelJunker May 8, 2019
ce85bed
set fixed dependencies versions
AxelJunker May 8, 2019
6bb897e
set mongoose options
AxelJunker May 8, 2019
47ab807
function for deleting docs before & after test
AxelJunker May 8, 2019
6b3621a
make tests work with mongoose@5.5.1
AxelJunker May 8, 2019
fde482b
different types in one index does not work in elastic > 6.x
AxelJunker May 8, 2019
c431b28
rename/include force-index-refresh test
AxelJunker May 8, 2019
a8925b4
filtered query not supported anymore, use bool instead
AxelJunker May 8, 2019
34899c1
sorting has to be made on the keyword field
AxelJunker May 8, 2019
56edab2
minor (generateMapping sync)
AxelJunker May 8, 2019
6d4ec22
use elasticsearch.js@15.2.0 (6.4 API is default, same as Travis)
AxelJunker May 8, 2019
e6b2015
remove unnescessary cleanTree object
AxelJunker May 16, 2019
2235308
do not map mixed fields
AxelJunker May 16, 2019
fe21149
return mapping
AxelJunker May 16, 2019
44f7e12
if field is mixed, keep field name, but remove type
AxelJunker May 16, 2019
a140b97
update mixed test
AxelJunker May 16, 2019
ace277d
don’t map mixed fields
AxelJunker May 16, 2019
0967de6
test mixed all the way
AxelJunker May 16, 2019
7f3c322
consistent return of schema object
AxelJunker May 21, 2019
f4566b1
minor
AxelJunker May 21, 2019
ff2e762
Merge branch 'develop' into develop
AxelJunker May 24, 2019
86c13e4
bug fix
AxelJunker May 24, 2019
b469680
check if elastic is up and running before tests
AxelJunker Jun 12, 2019
99ecd6e
add comment
AxelJunker Jun 12, 2019
1979856
disable alternative index and type
AxelJunker Jun 12, 2019
0c67758
use .toObject() before serialize
AxelJunker Jun 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_install:
- docker pull docker.elastic.co/elasticsearch/elasticsearch:6.4.2
- docker ps -a
- docker run -p 9200:9200 -p 9300:9300 -d -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.4.2
- until curl --silent -XGET --fail http://localhost:9200; do printf '.'; sleep 1; done

services:
- mongodb
Expand Down
40 changes: 32 additions & 8 deletions lib/mongoosastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ function createEsClient (options) {
return new elasticsearch.Client(esOptions)
}

function filterMappingFromMixed (props) {
const filteredMapping = {}
Object.keys(props).map((key) => {
const field = props[key]
if (field.type !== 'mixed') {
filteredMapping[key] = field
if (field.properties) {
filteredMapping[key].properties = filterMappingFromMixed(field.properties)
if (!Object.keys(filteredMapping[key].properties).length) {
delete filteredMapping[key].properties
}
}
}
})
return filteredMapping
}

function createMappingIfNotPresent (options, cb) {
const client = options.client
const indexName = options.indexName
Expand All @@ -46,6 +63,9 @@ function createMappingIfNotPresent (options, cb) {
const completeMapping = {}
completeMapping[typeName] = generator.generateMapping(schema)

const filtered = filterMappingFromMixed(completeMapping[typeName].properties)
completeMapping[typeName].properties = filtered

if (properties) {
Object.keys(properties).map(key => {
completeMapping[typeName].properties[key] = properties[key]
Expand All @@ -64,7 +84,9 @@ function createMappingIfNotPresent (options, cb) {
index: indexName,
type: typeName,
body: completeMapping
}, cb)
}, (err) => {
cb(err, completeMapping[typeName])
})
}
return client.indices.create({
index: indexName,
Expand All @@ -78,7 +100,9 @@ function createMappingIfNotPresent (options, cb) {
index: indexName,
type: typeName,
body: completeMapping
}, cb)
}, (err) => {
cb(err, completeMapping[typeName])
})
})
})
}
Expand Down Expand Up @@ -355,7 +379,6 @@ function Mongoosastic (schema, pluginOpts) {
let serialModel
let cb = inCb
let opts = inOpts
let _serialize = serialize

if (arguments.length < 2) {
cb = inOpts || nop
Expand All @@ -366,10 +389,6 @@ function Mongoosastic (schema, pluginOpts) {
return this.unIndex(cb)
}

if (typeof customSerialize === 'function') {
_serialize = customSerialize
}

setIndexNameIfUnset(this.constructor.modelName)

index = opts.index || indexName
Expand All @@ -378,7 +397,12 @@ function Mongoosastic (schema, pluginOpts) {
/**
* Serialize the model, and apply transformation
*/
serialModel = _serialize(this, mapping)
if (typeof customSerialize === 'function') {
serialModel = customSerialize(this, mapping)
} else {
serialModel = serialize(this.toObject(), mapping)
}

if (transform) serialModel = transform(serialModel, this)

const _opts = {
Expand Down
4 changes: 2 additions & 2 deletions test/alternative-index-method-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Index Method', function () {

// This does not work in elastic > 6.x
// Indices created in 6.x only allow a single-type per index
it('should be able to index to alternative index and type', function (done) {
/* it('should be able to index to alternative index and type', function (done) {
Tweet.findOne({
message: 'I know kung-fu!'
}, function (err, doc) {
Expand All @@ -98,5 +98,5 @@ describe('Index Method', function () {
}, config.INDEXING_TIMEOUT)
})
})
})
}) */
})
48 changes: 48 additions & 0 deletions test/mapping-generator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,54 @@ describe('MappingGenerator', function () {
should.not.exist(mapping.properties.name.properties.birthYear)
done()
})

// make this cleaner
it('should not map type mixed on mixed fields', function (done) {
// instead, Elastic should "guess" and set default mapping
const schema = new Schema({
string: String,
mixed_field: {
type: mongoose.Schema.Types.Mixed
},
mixed_arr_field: {
type: [mongoose.Schema.Types.Mixed]
},
obj_mixed: {
mixed: {
type: mongoose.Schema.Types.Mixed
}
}
})
const mongoosastic = require('../lib/mongoosastic')
schema.plugin(mongoosastic)

const MyModel = mongoose.model('MyModel', schema)

MyModel.createMapping((err, mapping) => {
if (err) console.log(err)
const doc = new MyModel({
string: 'test_string',
mixed_field: 'mixed',
mixed_arr_field: [1, 2],
obj_mixed: { mixed: 'nested mixed' }
})
const config = require('./config')
mongoose.connect(config.mongoUrl, config.mongoOpts, function () {
doc.save(() => {
setTimeout(() => {
MyModel.search({ query_string: { query: 'mixed' } }, (err, res) => {
res.hits.hits[0]._source.mixed_field.should.eql('mixed')
res.hits.hits[0]._source.mixed_arr_field.should.eql([1, 2])
res.hits.hits[0]._source.obj_mixed.mixed.should.eql('nested mixed')
doc.remove(() => {
config.deleteIndexIfExists('mymodels', done)
})
})
}, config.INDEXING_TIMEOUT)
})
})
})
})
})

describe('elastic search fields', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('serialize', function () {
first: 'Jeffrey',
last: 'Lebowski'
}
})
}).toObject()

it('should serialize a document with missing bits', function () {
const serialized = serialize(millionnaire, mapping)
Expand Down