【问题标题】:How can I get an hit without a exact match with es 2.X? (Python elasticsearch)在没有与 es 2.X 完全匹配的情况下,如何获得成功? (Python 弹性搜索)
【发布时间】:2017-01-15 11:22:08
【问题描述】:

我对弹性搜索有疑问。索引中有一个项目(“'title': 'Using Python with 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。附上代码。那么,当我使用“Using Python with”之类的查询进行搜索时,我如何才能获得成功,而没有完全匹配的查询。谢谢!

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

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    使用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 但你说你想要 exact 词组匹配。

    【讨论】:

    • 我知道模糊查询可以实现这个功能。但我想实现全文搜索。谢谢!
    • 谢谢!有用!但是我想实现全文搜索。使用查询“Python with Elasticsearch”,它什么也没有。
    • 好的,那你需要一个正则表达式查询,我加了一个例子。如果它解决了您的问题,请接受答案。
    • 非常感谢您的帮助!我主要是全文搜索有问题。对于这个官方教程,elastic.co/guide/en/elasticsearch/guide/current/…我的实验结果是"{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0}, "hits":{"total":0,"max_score":null,"hits":[]}} " 全文搜索好像不行!谢谢!
    • 您尝试过我在最新编辑中建议的wildcard 查询吗?!
    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2011-02-14
    相关资源
    最近更新 更多