|
@@ -14,6 +14,7 @@ import { SearchDelegatorName } from '~/interfaces/named-query';
|
|
|
import {
|
|
import {
|
|
|
MetaData, SearchDelegator, Result, SearchableData, QueryTerms,
|
|
MetaData, SearchDelegator, Result, SearchableData, QueryTerms,
|
|
|
} from '../../interfaces/search';
|
|
} from '../../interfaces/search';
|
|
|
|
|
+import ElasticsearchClient from './elasticsearch-client';
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:service:search-delegator:elasticsearch');
|
|
const logger = loggerFactory('growi:service:search-delegator:elasticsearch');
|
|
|
|
|
|
|
@@ -100,16 +101,19 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
|
|
|
initClient() {
|
|
initClient() {
|
|
|
const { host, auth, indexName } = this.getConnectionInfo();
|
|
const { host, auth, indexName } = this.getConnectionInfo();
|
|
|
|
|
|
|
|
- this.client = new this.elasticsearch.Client({
|
|
|
|
|
|
|
+ this.client = new ElasticsearchClient(new this.elasticsearch.Client({
|
|
|
node: host,
|
|
node: host,
|
|
|
ssl: { rejectUnauthorized: this.configManager.getConfig('crowi', 'app:elasticsearchRejectUnauthorized') },
|
|
ssl: { rejectUnauthorized: this.configManager.getConfig('crowi', 'app:elasticsearchRejectUnauthorized') },
|
|
|
auth,
|
|
auth,
|
|
|
requestTimeout: this.configManager.getConfig('crowi', 'app:elasticsearchRequestTimeout'),
|
|
requestTimeout: this.configManager.getConfig('crowi', 'app:elasticsearchRequestTimeout'),
|
|
|
- // log: 'debug',
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ }));
|
|
|
this.indexName = indexName;
|
|
this.indexName = indexName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ getType() {
|
|
|
|
|
+ return this.isElasticsearchV6 ? 'pages' : '_doc';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* return information object to connect to ES
|
|
* return information object to connect to ES
|
|
|
* @return {object} { host, auth, indexName}
|
|
* @return {object} { host, auth, indexName}
|
|
@@ -307,7 +311,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async createIndex(index) {
|
|
async createIndex(index) {
|
|
|
- const body = this.isElasticsearchV6 ? require('^/resource/search/mappings.json') : require('^/resource/search/mappings-es7.json');
|
|
|
|
|
|
|
+ const body = this.isElasticsearchV6 ? require('^/resource/search/mappings-es6.json') : require('^/resource/search/mappings-es7.json');
|
|
|
return this.client.indices.create({ index, body });
|
|
return this.client.indices.create({ index, body });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -344,7 +348,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
|
|
|
const command = {
|
|
const command = {
|
|
|
index: {
|
|
index: {
|
|
|
_index: this.indexName,
|
|
_index: this.indexName,
|
|
|
- _type: this.isElasticsearchV6 ? 'pages' : '_doc',
|
|
|
|
|
|
|
+ _type: this.getType(),
|
|
|
_id: page._id.toString(),
|
|
_id: page._id.toString(),
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
@@ -380,7 +384,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
|
|
|
const command = {
|
|
const command = {
|
|
|
delete: {
|
|
delete: {
|
|
|
_index: this.indexName,
|
|
_index: this.indexName,
|
|
|
- _type: this.isElasticsearchV6 ? 'pages' : '_doc',
|
|
|
|
|
|
|
+ _type: this.getType(),
|
|
|
_id: page._id.toString(),
|
|
_id: page._id.toString(),
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
@@ -607,16 +611,17 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
|
|
|
logger.debug('ES returns explanations: ', result.explanations);
|
|
logger.debug('ES returns explanations: ', result.explanations);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const { body } = await this.client.search(query);
|
|
|
|
|
const { body: result } = await this.client.search(query);
|
|
const { body: result } = await this.client.search(query);
|
|
|
|
|
|
|
|
// for debug
|
|
// for debug
|
|
|
logger.debug('ES result: ', result);
|
|
logger.debug('ES result: ', result);
|
|
|
|
|
|
|
|
|
|
+ const totalValue = this.isElasticsearchV6 ? result.hits.total : result.hits.total.value;
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
meta: {
|
|
meta: {
|
|
|
took: result.took,
|
|
took: result.took,
|
|
|
- total: result.hits.total,
|
|
|
|
|
|
|
+ total: totalValue,
|
|
|
results: result.hits.hits.length,
|
|
results: result.hits.hits.length,
|
|
|
},
|
|
},
|
|
|
data: result.hits.hits.map((elm) => {
|
|
data: result.hits.hits.map((elm) => {
|