python中的elasticsearch查询不会返回所有结果

83qze16e  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(0)|浏览(265)

我是python和elasticsearch的新手,我已经编写了elasticsearch python查询代码,它必须接收一些数据(关键字或类别)并向es数据库发送查询;然后在我的控制台中检索并打印匹配的数据:

def SingleKeywordQuery (keyword):
    response = es.search(index="main-news-test-data",body={"from":0,"size":10000,"query":{"multi_match":{
            "query":      keyword,
            "fields":     [ "content", "title", "lead" ]
        }}})
    return(response)

def SingleCategoryQuery (category):
   response = es.search(index="main-news-test-data",body={"from":0,"size":10000,"query":{"match":{"category": category}}})
   return(response)

if __name__ == '__main__': 
    keyword = input('Enter Keyword: ')
    category = input('Enter Category: ')
    mapCategory = Mapper(category)
...
    elif keyword != '' and mapCategory is None:
      data = SingleKeywordQuery(keyword)
      print(data)

    elif keyword == '' and mapCategory is not None:
      mapCategory = eval(mapCategory)
      print(mapCategory)
      data = SingleCategoryQuery(mapCategory)
      print(data)

    elif keyword == '' and mapCategory is None:
      print('Please select a filter')

问题是,我的代码只检索其中一个结果。比如,我知道有3个数据和我想要的分类号,或者2个数据和我想要的关键字,但是它只返回一个。有什么问题吗?
编辑:elasticsearch的数据Map

PUT /main-news-test-data
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      },
      "title": {
        "type": "text"
      },
      "lead": {
        "type": "text"
      },
      "agency": {
        "type": "keyword"
      },
      "date_created": {
        "type": "date"
      },
      "url": {
        "type": "keyword"
      },
      "image": {
        "type": "keyword"
      },
      "category": {
        "type": "keyword"
      },
      "id":{
        "type": "keyword"
      }
    }
  }
}

这是我用批量api上传的批量数据:

{ "index" : { "_index" : "main-news-test-data", "_id" : "1" } }
{
  "content":"\u0641\u0647\u06cc\u0645\u0647 \u062d\u0633\u0646\u200c\u0645\u06cc\u0631\u06cc: \u0627\u06af\u0631\u0686\u0647 \u062f\u0631 \u0647\u06cc\u0627\u0647\u0648\u06cc ",
        "title":"\u06a9\u0627\u0631\u0647\u0627\u06cc \u0642\u0627\u0644\u06cc\u0628\u0627\u0641",
        "lead":"\u062c\u0627\u0645\u0639\u0647 > \u0634\u0647\u0631\u06cc -.",
        "agency":"13",
        "date_created":1494518193,
        "url":"http://www.khabaronline.ir/(X(1)S(bud4wg3ebzbxv51mj45iwjtp))/detail/663749/society/urban",
        "image":"uploads/2017/05/11/1589793661.jpg",
        "category":"15",
        "id":"2981643"
}
{ "index" : { "_index" : "main-news-test-data", "_id" : "2" } }
{ 
....

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题