Elasticsearch NodeJS搜索:如何从响应中检索结果

时间:2016-02-11 15:06:32

标签: javascript node.js elasticsearch elasticsearch-2.0

我正在尝试使用节点js在Elasticsearch中搜索。这是我的剧本

var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: Infinity,

  // undocumented params are appended to the query string
  hello: "elasticsearch!"
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
    getmeres(client);

  }
});

function getmeres(client)
{
client.search({
      index: 'researchtest',
      body: {

              "aggs": {
                "docs": {
                  "terms": {
                    "field": "DocumentID",
                    "size": 0
                  }
                }
              }

      }
    }, function (error, response) {
        if (error) {
            console.trace('Search query failed');
          } else {
            console.log('All is well');
            d=response;//console.log(response);
            showdocs(d)
          }
    });
}

function showdocs(d){
console.log(d["hits"]);}

我得到了

All is well
{ total: 92,
  max_score: 1,
  hits: 
   [ { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2axzrLgN-DZLLsmp',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2izQrLgN-DZLLsnw',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2EEnrLgN-DZLLsjj',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2F7MrLgN-DZLLsj6',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2nhDrLgN-DZLLsol',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2pMUrLgN-DZLLsox',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2mTMrLgN-DZLLsoL',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2rDZrLgN-DZLLspS',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2t5ErLgN-DZLLsp8',
       _score: 1,
       _source: [Object] },
     { _index: 'researchtest',
       _type: 'test',
       _id: 'AVLC2dVHrLgN-DZLLsnA',
       _score: 1,
       _source: [Object] } ] }

但是我没有看到我想要的实际值。我如何从响应中获取值?在控制台中我也看到了

"aggregations": {
      "docs": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "2235",
            "doc_count": 2
          },
          {
            "key": "12312",
            "doc_count": 2
          },
          {
            "key": "4565",
            "doc_count": 2
          },
          {
            "key": "7809780",
            "doc_count": 2
          },
          {
            "key": "8678",
            "doc_count": 2
          },

继续列出所有匹配的结果

  1. 我只想要唯一的值,我该如何指定聚合?

  2. 如何从上述查询的结果中提取实际值或keys

  3. 修改

    所以我有点想通了。我可以通过

    访问
    function showdocs(d){
        da=d.hits.hits
    
        for (i=0;i<da.length;i++)
            {
    console.log(da[i]["_source"]["DocumentID"]);}}
    

    但这只能给我10个结果!我确信我在那个领域有50个不同的值

2 个答案:

答案 0 :(得分:2)

使用大小0来获取聚合桶,

class LoginViewController: UIViewController {

@IBOutlet weak var textFieldPassword: UITextField!
@IBOutlet weak var textFieldEmail: UITextField!

var authStateListener: AuthStateDidChangeListenerHandle?

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    authStateListener = Auth.auth().addStateDidChangeListener { (auth, user) in
        if user != nil {
            self.callMainController()
        }
    }
}

func callMainController() {
    if let storyboard = self.storyboard {
        let viewController = storyboard.instantiateViewController(withIdentifier: "MainController") as! UITabBarController
        self.present(viewController, animated: true, completion: nil)
    } else {
        Alert(controller: self).show(message: "Error on show MainController")
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    Auth.auth().removeStateDidChangeListener(authStateListener!)
}

@IBAction func didTapLogin(_ sender: Any) {
    if let email = self.textFieldEmail.text, let password = self.textFieldPassword.text {
        Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            print("User Logged")
        }
    }
}

答案 1 :(得分:1)

您可以在搜索查询中使用size参数。如果未指定,则大小默认为10。请参阅下面添加的尺寸参数。

client.search({       index:&#39; researchtest&#39;,       大小:100       ...............