2015-09-12 118 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的帮助 –