0

これが私の目的です。タスク内のテキスト、つまりtask.nameを検索したい。

{
  "_id" : ObjectId("55e2acd77e5e4ddc037b3ef5"),
  "userId" : "123",
  "username" : "user12",
  "address" : "abc",
  "number" : 928228828282.0,
  "task" : [{
      "name" : "metac",
      "productCode" : "1234",
      "_id" : ObjectId("55e2acd77e5e4ddc037b3ef7")
    }, {
      "name" : "alfa33",
      "productCode" : "1234",
      "_id" : ObjectId("55e2acd77e5e4ddc037b3ef6")
    }],
  "__v" : 0
}

そのため、クエリを実行すると、すべてのタスクが返されます。

curl -XPOST 'localhost:9200/userprofiles/_search?pretty' -d '
{
  "query": { "match": { "name": "alfa33" } }
}

出力:

{
    "took": 51,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.19178301,
        "hits": [
            {
                "_index": "userprofiles",
                "_type": "userprofile",
                "_id": "55e2acd77e5e4ddc037b3ef5",
                "_score": 0.19178301,
                "_source": {
                    "userId": "123",
                    "username": "user12",
                    "address": "abc",
                    "number": 928228828282,
                    "task": [
                        {
                            "name": "metac"
                        },
                        {
                            "name": "alfa33"
                        }
                    ]
                }
            }
        ]
    }
}

ご覧のとおり、タスクは完全な配列を返します。選択したタスクは 1 つだけです。

ノード内でmongoosasticを使用していると問題が発生するため、リクエストをElasticsearchに直接使用しようとしました。

mongoosastic config - > elasticsearch search text return full array issueこの解決策を試しましたが、機能しませんでした。

現在、検索機能を使用せずにgit Bushでcurlコマンドを使用して結果を検索しています

編集

FILE:- マングースとモングーサスティック。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var medicineSchema = require('./search')
var mongoosastic = require("mongoosastic");

var UserProfileSchema = new Schema({
    userId: String,
    username: String,
    address: String,
    number: Number,
    task: [{
        name: {
            type: String,
            es_boost: 2.0 // or es_indexed:true
        },
        taskCode: String,
    }]
});
UserProfileSchema.plugin(mongoosastic);
UserProfileSchema.plugin(mongoosastic, {
    host: "localhost",
    port: 9200,
    //  ,curlDebug: true
});
UserProfile = module.exports = mongoose.model('UserProfile', UserProfileSchema);
UserProfile.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 UserProfileSchema = require('../../app/models/user');
 UserProfileSchema.search({
        query_string: {
            query: name
        }
    }, function(err, result) {
        if (err) {
            callback({
                RESULT_CODE: '-1',
                MESSAGE: 'System error'
            });
        } else {
            callback({
                RESULT_CODE: '1',
                DATA: result
            });
        }
    });
4

1 に答える 1