如何使用elasticsearchclient搜索内部数组

时间:2012-06-09 07:50:09

标签: node.js couchdb elasticsearch

我已经安装了elasticsearch-river-couchdb和node elasticsearchclient。现在我使用node elasticsearchclient在couchdb中搜索数据。我有这样的JSON结构,

{   
    first_name: "aruna", 
    interests: [ {name: "pets", level: 5}, 
                 {topic: "es", degree: "strong"}
              ] ,
designation: "SE",
company: "DOM",
salary: 200000
}

现在我需要搜索我在查询中给出的任何值。为此,我尝试了下面的代码,

var searchText = '*aru*'
testSearch = function() {
var qryObj = {
    "size" : 50,
            "query" : { 
           "wildcard" : { 
                "_all" :searchText
                }
             } 
     };
elasticSearchClient.search("test", "",qryObj, size)
    .on('data', function(data) {
        console.log("Search: "+data)
        assert.ok(JSON.parse(data), "testSearch failed.")
    })
    .exec()
}

如果我将searchText的值赋予aruna或SE或DOM或200000,我可以获得完整的文档。但是,当我搜索宠物或强或5或es时,我没有得到该文件。请任何人都可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:0)

我没有使用节点,但是一旦我使用curl创建了索引:

curl -XPOST 'http://localhost:9200/test/contact/' -d '
{   
first_name: "aruna", 
interests: [ {name: "pets", level: 5}, 
             {topic: "es", degree: "strong"}
          ] ,
designation: "SE",
company: "DOM",
salary: 200000
}
'
{"ok":true,"_index":"test","_type":"contact","_id":"B1it8B1wTqGkx3qSQqSo1Q","_version":1}

我可以使用query_string搜索字符串宠物:

curl -XGET http://localhost:9200/test/contact/_search -d '
{"query":{"bool":{"must":[{"query_string":    {"default_field":"_all","query":"pets"}}],"must_not":[],"should":      []}},"from":0,"size":50,"sort":[],"facets":{}}
'       

{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":    {"total":1,"max_score":0.06780553,"hits":         [{"_index":"test","_type":"contact","_id":"B1it8B1wTqGkx3qSQqSo1Q","_score":0.06780553,   "_source" : 
{   
first_name: "aruna", 
interests: [ {name: "pets", level: 5}, 
             {topic: "es", degree: "strong"}
          ] ,
designation: "SE",
company: "DOM",
salary: 200000
}

注意:当您在字段内创建带有哈希的索引时,它被称为嵌套索引,我不确定通配符查询是否嵌套字段。但是查询字符串呢。同样在19.4中,您可以使用特殊语法在query_string中查询嵌套字段,在您的情况下: 利益*:宠物

http://www.elasticsearch.org/blog/2012/05/21/0.19.4-released.html