【问题标题】:Elasticsearch Index Data giving wrong results in queryElasticsearch 索引数据在查询中给出错误的结果
【发布时间】:2018-06-27 02:11:42
【问题描述】:

我正在尝试使用 Elasticsearch 为我的 react web 应用程序创建一个全文搜索,但它没有返回查询的所有结果。我认为我在批量方法中犯了一些错误,但我无法理解我做错了什么。 我的代码是:

// Declare variable to contain body of JSON data for loading to ElasticSearch

                    let br = [];

// Function to create body for loading to ElasticSearch
                    function create_bulk (bulk_request) {
                        let obj;

                        for (let i = 0; i < res.length; i++) {
                            obj = res[i];
                            // Insert header of record
                            bulk_request.push({index: {_index: 'tweet', _type: 'tweet', _id: i+1}});
                            bulk_request.push(obj);
                        }
                        return bulk_request;
                    }

// Call function to get body for loading
                    create_bulk(br);

// Standard function of ElasticSearch to use bulk command
                    client.bulk(
                        {
                            body : br
                        }, function (err, resp) {
                            console.log(err);
                        });

                    client.search({
                        index: 'tweet',
                        type: 'tweet',
                        body: {
                            query: {
                                match: {"text" : "new york" }
                            },
                        }
                    },function (error, response,status) {
                        if (error){
                            console.log("search error: "+error)
                        }
                        else {
                            console.log("--- Response ---");
                            console.log(response);
                            console.log("--- Hits ---");
                            response.hits.hits.forEach(function(hit){
                                console.log(hit);
                            })
                        }
                    });

数据样本,它是一个 JSON 数据数组:

{date: "2014-06-04 11:30:07", text: "New York Today: Honoring our Civil Servants t.co/Lhz9fgt2FW", user_id: "nytimes "}

【问题讨论】:

  • 在批量调用和搜索调用之间,您需要调用刷新以确保所有索引数据都可用于搜索。
  • 我该怎么做?
  • @Val 它得到了很好的索引,因为它返回 10 个结果,这是我猜的限制。如果您对节点有任何想法,请告诉我如何更改该限制?我从 localhost 尝试过,更改大小后返回所有结果。
  • 是的,只需在您的搜索电话中添加size: 100
  • 谢谢,有什么方法可以包含所有搜索吗?如果没有,那么我将使用一些巨大的数字。

标签: javascript node.js reactjs elasticsearch


【解决方案1】:

在批量调用和搜索调用之间,您需要调用刷新以确保所有索引数据都可用于搜索。

                client.bulk(
                    {
                        refresh: true,            <--- add this
                        body : br
                    }, function (err, resp) {
                        console.log(err);

                        client.search({...})      <--- and make your search call only when the bulk returns

                    });

【讨论】:

  • 科尔,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多