如果没有与es 2.X的精确匹配,我怎么能获得命中率?(Python elasticsearch)

2024-04-25 08:04:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我对elasticsearch有问题。索引中有一个项(“'title':'Using Python with Elasticsearch'”)。我只能得到返回的结果,只有我搜索的确切查询。但是,当我搜索“'title':'Using Python with'”时,代码什么也打不到。
es版本是:{u'cluster\u name':u'elasticsearch',u'tagline':u'You Know,for Search',u'version':{u'lucene\u version':u'5.4.1',u'build\u hash':u'd045fc29d1932bce18b2e65ab8b297fbf6cd41a1',u'number':u'2.2.1',u'build\u timestamp':u'2016-03-09T09:38:54Z',u'build\u snapshot':False},u'name':u'Lorelei Travis'}
如果我是对的,应该是es2.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

Tags: 代码namebuildindexestitlewithbody
1条回答
网友
1楼 · 发布于 2024-04-25 08:04:15

使用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不匹配,但您说过希望短语匹配。你知道吗

相关问题 更多 >