Elasticsearch长查询无法搜索

时间:2015-04-14 14:15:19

标签: curl elasticsearch

我的查询示例:

"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

如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

要尝试的一件事是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"}

现在我要添加一个"query_terms"文档和PUT

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