2015-09-12 141 views
0

我是彈性搜索的新手。我正在試圖用Python在我的大學項目中實現它。我想使用Elastic搜索作爲簡歷索引器。一切工作正常,除了它顯示在_source field的所有領域。我不想要一些領域,我嘗試了太多的東西,但沒有任何工作。下面是我的代碼彈性搜索沒有顯示字段

es = Elastcisearch() 
    query = { 
"_source":{ 
    "exclude":["resume_content"] 
      }, 
     "query":{ 
      "match":{ 
       "resume_content":{ 
        "query":keyword, 
        "fuzziness":"Auto", 
        "operator":"and", 
        "store":"false" 
         } 
        } 
       } 
      } 

    res = es.search(size=es_conf["MAX_SEARCH_RESULTS_LIMIT"],index=es_conf["ELASTIC_INDEX_NAME"], body=query) 

回報水庫

其中es_conf是我的本地詞典。

除了上面的代碼,我也試過_source:false_source:[name of my fields]fields:[name of my fields]。我也在我的搜索方法中嘗試了store=False。有任何想法嗎?

回答

1

您是否嘗試過使用fields

下面是一個簡單的例子。我設置了一個映射有三個字段,(想象力)命名爲"field1""field2""field3"

PUT /test_index 
{ 
    "mappings": { 
     "doc": { 
     "properties": { 
      "field1": { 
       "type": "string" 
      }, 
      "field2": { 
       "type": "string" 
      }, 
      "field3": { 
       "type": "string" 
      } 
     } 
     } 
    } 
} 

然後我索引的三個文件:

POST /test_index/doc/_bulk 
{"index":{"_id":1}} 
{"field1":"text11","field2":"text12","field3":"text13"} 
{"index":{"_id":2}} 
{"field1":"text21","field2":"text22","field3":"text23"} 
{"index":{"_id":3}} 
{"field1":"text31","field2":"text32","field3":"text33"} 

而且我們說,我想找到包含"text22"文檔在"field2"字段中,但我只想返回"field1"和「field2」的內容。這裏的查詢:

POST /test_index/doc/_search 
{ 
    "fields": [ 
     "field1", "field2" 
    ], 
    "query": { 
     "match": { 
      "field2": "text22" 
     } 
    } 
} 

返回:

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1.4054651, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "doc", 
      "_id": "2", 
      "_score": 1.4054651, 
      "fields": { 
       "field1": [ 
        "text21" 
       ], 
       "field2": [ 
        "text22" 
       ] 
      } 
     } 
     ] 
    } 
} 

這是我使用的代碼:http://sense.qbox.io/gist/69dabcf9f6e14fb1961ec9f761645c92aa8e528b

它應該很容易與Python的適配器設置它。

+0

我試過你的代碼..現在我的代碼工作很好thanx的幫助 –