python - 如何在不完全匹配es 2.X的情况下获得成功? (Python弹性搜寻)

标签 python elasticsearch

我对elasticsearch有问题。索引中有一个项目(“标题”:“将Python与Elasticsearch一起使用”)。我只有获得确切的查询,才能得到返回的结果。但是,当我搜索“'title':'Using Python with'”时,代码什么也打不上。
es版本为:{u'cluster_name':u'elasticsearch',u'tagline':u'You Know,for Search',u'version':{u'lucene_version':u'5.4.1',u' build_hash':u'd045fc29d1932bce18b2e65ab8b297fbf6cd41a1',u'number':u'2.2.1',u'build_timestamp':u'2016-03-09T09:38:54Z',u'build_snapshot':False},u'name' :u'Lorelei Travis'}
如果我是对的,则应为es 2.2.1。该代码已附加。因此,当我使用“与Python一起使用”之类的查询进行搜索而没有完全匹配查询时,如何获得成功。谢谢!

INDEX_NAME = 'test_11'
from elasticsearch import Elasticsearch
es = Elasticsearch()
print es.info()

request_body = {  
      "mappings":{  
        "post":{  
          "properties":{  
            "title":{  
              "type":"string",
              "index":"analyzed"
            }
          }
        }
      }
    }

if es.indices.exists(INDEX_NAME):
    res = es.indices.delete(index = INDEX_NAME)
    print(" response: '%s'" % (res))

res = es.indices.create(index = INDEX_NAME, body=request_body)
print res

es.index(index=INDEX_NAME, doc_type='post', id=1, body={
  'title': 'Using Python with Elasticsearch'
  }
)

es.indices.refresh(index=INDEX_NAME)

res = es.search(index=INDEX_NAME, body={'query': {'match': {'title': 'Using Python with Elasticsearch'}}})
#res = es.search(index=INDEX_NAME, body ={"query":{"match":{"title":"Using Python with"}}})

print '\n'
res = es.indices.get(index=INDEX_NAME)
print res

最佳答案

使用prefix查询?如果您想要更高级的功能,请考虑使用regexp查询或fuzzy查询。

编辑:如果我错了,请纠正我:您希望Using Python with匹配所有结果,例如Using Python with ElasticsearchUsing Python with Lucene吗?然后是一个像这样的映射:

request_body = {  
  "mappings":{  
    "post":{  
      "properties":{  
        "title":{  
          "type":"string",
          "index":"not_analyzed"
        }
      }
    }
  }
}

然后是一个查询,如:
{
    "query": {
        "prefix": {
            "title": "Using Python with"
        }
    }
}

应返回所有相关文件。
注意,我将index字段更改为not_analyzed

更多here

编辑2:
如果要匹配在目标字段中的任意位置包含精确查询的所有文档,而不仅仅是前缀,请按照最初建议的方式使用regexp查询。然后
{
    "query": {
        "wildcard": {
            "name": "Using Python with*"
        }
    }
}

将像前缀查询一样工作,并且匹配Using Python with ElasticsearchUsing Python with Lucene,但是
{
    "query": {
        "wildcard": {
            "name": "*Python with Elasticsearch"
        }
    }
}

仅匹配Using Python with Elasticsearch。它不会匹配Using Python with elasticsearch,但您说过想要完全匹配的词组。

关于python - 如何在不完全匹配es 2.X的情况下获得成功? (Python弹性搜寻),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660525/

相关文章:

python - binascii 出现问题。错误 : Odd-length string

Python Git 模块经验?

python - 导入路径 - 正确的方式?

python-2.7 - 利用ES功能批量插入后删除重复项

elasticsearch - Elasticsearch-如何在一个字符串中搜索多个单词

python - Pandas 更改列日期格式

python - 在 Selenium Web 驱动程序上发送键 shift+tab

node.js - 请求包含无法识别的参数:[type] in elastic search while making request for getting suggestion

azure - Elk Logstash 无法连接到事件中心 Azure

python - Elasticsearch 批量获取搜索结果?