2016-05-31 57 views
1

记录Elasticsearch存储:和查询elasticsearch蟒蛇

"hits" : [ { 
     "_index" : "test", 
     "_type" : "test_doc", 
     "_id" : "AVUHBSESPB1nLi5k2Dxp", 
     "_score" : 1.0, 
     "_source":{"valid": true,"action_taken": false} 
    }, { 
     "_index" : "test", 
     "_type" : "test_doc", 
     "_id" : "AVUHBI1IPB1nLi5k2Dxo", 
     "_score" : 1.0, 
     "_source":{"valid": true,"action_taken": false} 
    }, { 
     "_index" : "test", 
     "_type" : "test_doc", 
     "_id" : "AVUHWFipPB1nLi5k2Dxu", 
     "_score" : 1.0, 
     "_source":{"valid": false,"action_taken": false} 
    } ] 

我们需要找到那些记录有效的真实,ACTION_TAKEN使用elasticsearch Python包是python脚本错误。

我们正在尝试使用

resp = ES.search(index='test', doc_type='test_doc', q='action_taken:0 valid:1) 

但是执行上做同样的,它返回的所有记录。 是否有任何可能的方式使用搜索来做同样的事情?

PS:我试图编写一个API调用,它根据请求中提供的json数据提供的搜索条件来搜索记录。

回答

1

您发送给ElasticSearch的请求看起来不像一个有效的查询。 ES需要以特定的方式进行格式化查询:在此基础上

"query": { 
    "match": { 
     "content": term 
    } 
} 

,我相信你需要改变q PARAM。

对于ElasticSearch前2.x,查询应该使用filteredfilter选项。查询字符串是更类似于SQL查询,因此andor过滤器可(docs):

"query": { 
    "filtered": { 
     "filter": { 
      "and": [ 
       {"term": { "action_taken": 0 }}, 
       {"term": { "valid": 0 }} 
      ] 
     } 
    } 
} 

开始ElasticSearch 2.3,该and查询已被弃用,bool建议改为:

"query": { 
    "bool": { 
     "must": [ 
      {"term": { "action_taken": 0 }}, 
      {"term": { "valid": 0 }} 
     ] 
    } 
} 

Here is the link to the 2.3 docs

希望这会有所帮助。让我知道。

+0

所以我们应该需要通过上面提出的整个json作为查询参数在es搜索函数中调用resp = ES.search(index ='test',doc_type ='test_doc',query = ') – user3351750

+0

它的工作原理使用'resp = ES.search(index ='test',doc_type ='test_doc',body = )' – user3351750