【问题标题】:Elasticsearch long query fails to searchElasticsearch 长查询搜索失败
【发布时间】:2015-04-14 14:15:18
【问题描述】:

我的查询示例:

"query" : {
    "bool" : {
        "must" : [
            { "terms" : { "group_id" : ["1","2","3","4","5","6","7","8"]} }
        ]
    }
}

我对它进行 json 编码并使用 curl 发送

http://localhost:9200/document/_search?source=JSON_ENCODED_QUERY

上面的工作正常。

但如果我在查询中添加几百个组 ID,就会出现问题。

服务器响应:

curl: (52) Empty reply from server

我该如何解决我的问题?

【问题讨论】:

    标签: curl elasticsearch


    【解决方案1】:

    要尝试的一件事是terms lookup。它允许您指定另一个索引作为您正在搜索的术语的来源。这是一个简单的例子。

    首先,我创建了一个具有两种类型的索引。 "doc" 将是我们正在搜索的文档,"query_terms" 将用于存储我们要查询的术语。这是索引定义:

    PUT /test_index
    {
       "mappings": {
          "doc": {
             "properties": {
                "group_id": { "type": "integer" },
                "text": { "type": "string" }
             }
          },
          "query_terms": {
             "properties": {
                "values": { "type": "string" }
             }
          }
       }
    }
    

    然后我添加一些文档:

    POST /test_index/doc/_bulk
    {"index":{}}
    {"group_id":1,"text":"Lorem ipsum"}
    {"index":{}}
    {"group_id":2,"text":"dolor sit amet"}
    {"index":{}}
    {"group_id":3,"text":"consectetur adipiscing elit"}
    {"index":{}}
    {"group_id":4,"text":"Pellentesque eu nisi"}
    {"index":{}}
    {"group_id":5,"text":"sit amet."}
    {"index":{}}
    {"group_id":6,"text":"velit pellentesque"}
    {"index":{}}
    {"group_id":7,"text":"ornare eleifend a leo"}
    {"index":{}}
    {"group_id":8,"text":"Integer in aliquam turpis"}
    {"index":{}}
    {"group_id":9,"text":"Pellentesque sed"}
    {"index":{}}
    {"group_id":10,"text":"quam sit amet"}
    

    现在我将添加一个带有PUT"query_terms" 文档:

    PUT /test_index/query_terms/1
    {
        "values": [2,4,6,8]
    }
    

    我可以用它来查询文档:

    POST /test_index/doc/_search
    {
       "query": {
          "filtered": {
             "query": {
                "match_all": {}
             },
             "filter": {
                "terms": {
                   "group_id": {
                      "index": "test_index",
                      "type": "query_terms",
                      "id": "1",
                      "path": "values"
                   }
                }
             }
          }
       }
    }
    ...
    {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 4,
          "max_score": 1,
          "hits": [
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "d07TqRjjRvWW9MJOH1l13Q",
                "_score": 1,
                "_source": {
                   "group_id": 2,
                   "text": "dolor sit amet"
                }
             },
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "Qqy1idktQBqoKTR279DMXQ",
                "_score": 1,
                "_source": {
                   "group_id": 4,
                   "text": "Pellentesque eu nisi"
                }
             },
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "RoFzGAOhQxmetvbh8-weew",
                "_score": 1,
                "_source": {
                   "group_id": 6,
                   "text": "velit pellentesque"
                }
             },
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "IRH0qS9QQmWJEmgGMIr2SQ",
                "_score": 1,
                "_source": {
                   "group_id": 8,
                   "text": "Integer in aliquam turpis"
                }
             }
          ]
       }
    }
    

    对于这个例子,一个更简单的方法可以很好地工作,但这种方法应该扩展到非常长的术语列表。

    这是我使用的代码:

    http://sense.qbox.io/gist/cbaab881c5fa5fd7fd6dfc819e2bea82d09495e6

    【讨论】:

    • ID 是动态的,如果我使用这种方法,我每次发出搜索请求时都需要发布 term_query 值。
    • 我最终实施了您的解决方案,因为我没有找到更好的解决方案。我还必须禁用缓存。就过滤器“_cache”而言:false。在 elasticsearch.yml indices.cache.filter.terms.expire_after_write:0s。更多信息在这里elastic.co/guide/en/elasticsearch/reference/1.x/…
    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多