亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

elasticsearch 查詢返回的次數超過點擊次數

elasticsearch 查詢返回的次數超過點擊次數

神不在的星期二 2021-11-23 20:11:57
我的索引 python 文件下面應該有 7 個匹配基于真實數據的查詢,但它一直產生 10 個結果。因為默認大小參數是 10 有沒有辦法讓它產生與點擊次數一樣多的次數而不是大???還是我必須預測大小并一直將其放入查詢中?結果:也許與我如何索引它有關?idk 為什么總命中數是 26639。它應該像 7 一樣匹配。from elasticsearch import  helpers, Elasticsearchfrom datetime import datetimeimport csvimport jsones = Elasticsearch()with open('result.csv', encoding='utf-8') as f:    reader = csv.DictReader(f)    helpers.bulk(es, reader, index='hscate', doc_type='my-type')res = es.search(index = 'hscate',            doc_type = 'my-type',           # size ='1000',            #from_=0,                body = {                'query': {                         'match' : {                         'name' : '???? ??? ?????? 3+1?_? 4?'                    }                }            })print(len(res['hits']['hits']))with open('mycsvfile.csv', 'w',encoding='utf-8',newline='') as f:  # Just use 'w' mode in 3.x    header_present  = False    for doc in res['hits']['hits']:        my_dict = doc['_source']         if not header_present:            w = csv.DictWriter(f, my_dict.keys())            w.writeheader()            header_present = True        w.writerow(my_dict)
查看完整描述

2 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

根據我們作為評論的討論,我現在可以理解您的意思以及您的實際問題是什么。


當您在 elasticsearch 中使用默認值時,elasticsearch 正在使用標準分析器分析您的文本,該分析器基本上將您的文本拆分為標記。當您使用匹配查詢在該字段上進行搜索時,將應用相同的分析過程。這意味著您的查詢文本也被分解為標記。該match查詢運行“或”所有生成的標記。


您可以在 Kibana 開發者控制臺中復制和粘貼的以下示例顯示:


DELETE test

PUT test 

PUT test/_doc/1

{

  "name": "???? ??? ?????? 3+1?_? 4?"

}

PUT test/_doc/2

{

  "name": "???? ?????? 4?"

}

GET test/_search

{

  "query": {

    "match": {

      "name": "???? ??? ?????? 3+1?_? 4?"

    }

  }

}

它給出了以下結果:


{

  "took" : 12,

  "timed_out" : false,

  "_shards" : {

    "total" : 5,

    "successful" : 5,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : 2,

    "max_score" : 1.7260926,

    "hits" : [

      {

        "_index" : "test",

        "_type" : "_doc",

        "_id" : "1",

        "_score" : 1.7260926,

        "_source" : {

          "name" : "???? ??? ?????? 3+1?_? 4?"

        }

      },

      {

        "_index" : "test",

        "_type" : "_doc",

        "_id" : "2",

        "_score" : 0.8630463,

        "_source" : {

          "name" : "???? ?????? 4?"

        }

      }

    ]

  }

}

如果您沒有在索引設置中定義任何分析器,那么可能elasticsearch 生成了一個.keyword未分析的子字段。您可以像這樣查詢:


GET test/_search

{

  "query": {

    "term": {

      "name.keyword": "???? ??? ?????? 3+1?_? 4?"

    }

  }

}

這現在只提供完全匹配:


{

  "took" : 3,

  "timed_out" : false,

  "_shards" : {

    "total" : 5,

    "successful" : 5,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : 1,

    "max_score" : 0.2876821,

    "hits" : [

      {

        "_index" : "test",

        "_type" : "_doc",

        "_id" : "1",

        "_score" : 0.2876821,

        "_source" : {

          "name" : "???? ??? ?????? 3+1?_? 4?"

        }

      }

    ]

  }

}

如果您知道永遠不會運行全文搜索,而只會運行完全匹配,并且不需要對name字段進行聚合或排序,那么您可以按如下方式定義索引:


DELETE test

PUT test 

{

  "mappings": {

    "_doc": {

      "properties": {

        "name": {

          "type": "text",

          "analyzer": "keyword"

        }

      }

    }

  }

}

PUT test/_doc/1

{

  "name": "???? ??? ?????? 3+1?_? 4?"

}

PUT test/_doc/2

{

  "name": "???? ?????? 4?"

}

GET test/_search

{

  "query": {

    "term": {

      "name": "???? ??? ?????? 3+1?_? 4?"

    }

  }

}

這也給出了一個結果,并且比默認行為需要更少的磁盤空間。


查看完整回答
反對 回復 2021-11-23
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

正如您所懷疑的那樣,我認為 elasticsearch 會根據它們在您的數據中匹配它們時產生的排名,為您簡單地生成 10 個結果。


嘗試這個:


body = {

    'from': 0,

    'size': 1,

    'query': {

        'bool': {

            'must': [

                {

                    'match': {

                        'Category' : 'category name',

                    }

                },

                {

                    'match' : {

                        'name' : 'product name'

                    }

                }

            ]

        }

    }

}

來源:https : //www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html


查看完整回答
反對 回復 2021-11-23
  • 2 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號