内部数组的Elasticsearch全文查询问题 [英] Elasticsearch full-text query issue from inner array

查看:1359
本文介绍了内部数组的Elasticsearch全文查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的对象。我想要在任务中找到文本,例如 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)
$] b
$ _
$当我查询它会返回所有任务。

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

输出:

  {
took:51 ,
timed_out:false,
_shards:{
total:5,
成功:5,
失败: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,
任务:[
{
name:metac
},
{
name:alfa33
}
]
}
}
]
}
}

正如您所看到的任务r eturn full array我只想要选择1个任务。

我在节点内使用mongoosastic它会给我带来问题,因此我试图直接将请求用于Elasticsearch。

p>

mongoosastic config - > elasticsearch搜索文本返回完整数组问题我试过了这个解决方案,但不工作。



目前我搜索我的结果使用curl命令在git布什不使用搜索功能

<

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var medicineSchema = require('./ search')
var mongoosastic = require(mongoosastic);
$ b $ var UserProfileSchema =新架构({
userId:String,
username:String,
地址:字符串,$ b $ number:Number,
任务:[{
name:{
type:String,
es_boost:2.0 //或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('err,mapping){
if(err){
console.log('创建映射的错误(您可以放心地忽略)');
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
});
}
});


解决方案

为了将这些视为单独的对象,需要为任务字段使用嵌套类型
,因此请按照以下方式设置映射:

  { 
mappings:{
doc:{
...所有其他字段...:{},
任务:{
属性:{
_id:{
type:string
},
name:{
type:字符串

productCode:{
type:string
}
},
type:nested
}
}
}
}

后重新索引您的文档。您需要使用带有
内部匹配查询的嵌套查询来返回匹配查询的任务:

  {
query:{
嵌套:{
path:task,
query:{
match:{
name:alfa33
}
},
inner_hits:{}
}
}
}

inner_hits 部分将返回匹配的特定任务由
本身查询。



我用
此处输出全部内容,因为这是一个
的小短文,可以在这里完整发布。


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

解决方案

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.

这篇关于内部数组的Elasticsearch全文查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆