Elasticsearch full-text query issue from inner array

拜拜、爱过 提交于 2020-01-07 06:56:48

问题


This is my object. and I want find text inside of task i.e 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
}

so when I query for that it will return all task.

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

Output:

{
    "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"
                        }
                    ]
                }
            }
        ]
    }
}

As you can see task return full array I want only 1 task which is selected.

I am using mongoosastic inside node it will give me problem so i tried to use request directly to Elasticsearch.

mongoosastic config - >elasticsearch search text return full array issue i tried this solution but not working.

Currently I am search my result using curl command in git bush not using search function

EDIT

FILE:- mongoose and mongoosastic.

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

And my search Query:

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

回答1:


In order to treat these as separate objects, you will need to use a nested type for the "task" field, so setting up your mappings as follows:

{
    "mappings": {
        "doc": {
            "... all the other fields ...": {},
            "task": {
                "properties": {
                    "_id": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    },
                    "productCode": {
                        "type": "string"
                    }
                },
                "type": "nested"
            }
        }
    }
}

After reindexing your document. You'll need to use a nested query with an inner-hits query to return which task matched the query:

{
  "query": {
    "nested": {
      "path": "task",
      "query": {
        "match": {
          "name": "alfa33"
        }
      },
      "inner_hits": {}
    }
  }
}

The inner_hits portion will return the specific task that matched the query by itself.

I've posted an example with the full output here since it's a little long to post in entirety here.



来源:https://stackoverflow.com/questions/32294267/elasticsearch-full-text-query-issue-from-inner-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!