0

所以,我应该先说我理解 * 是一个特殊字符,应该为弹性搜索查询转义。这是我面临的设置和麻烦。基本问题归结为我无法搜索仅包含“*”的字段。

curl -XPUT 'http://localhost:9200/test_index/test_item/1' -d '{
    "some_text" : "*"
}'
curl -XPUT 'http://localhost:9200/test_index/test_item/2' -d '{
    "some_text" : "1+*"
}'
curl -XPUT 'http://localhost:9200/test_index/test_item/3' -d '{
    "some_text" : "asterisk"
}'

curl -XGET 'http://localhost:9200/test_index/_search?q=some_text:*'

Results:
"hits":{"total":2,"max_score":1.0,"hits":[
    "_source":{"some_text" : "1+*"},
    "_source":{"some_text" : "asterisk"}
]


curl -XGET 'http://localhost:9200/test_index/_search?q=some_text:\*'

Results:
"hits":{"total":0,"max_score":null,"hits":[]}

Using python elasticsearch:

>>>from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>>es.search(index='test_index', doc_type='test_item', body={"query":{"match":{"some_text":"*"}}})

No hits

>>>es.search(index='test_index', doc_type='test_item', body={"query":{"match":{"some_text":"asterisk"}}})

One hit('asterisk')

>>>es.search(index='test_index', doc_type='test_item', body={"query":{"match":{"some_text":"\*"}}})

No hits



Using pyelasticsearch
>>>es.search('some_text:*', index='test_index')
2 hits, '1+*' and 'asterisk'
>>>es.search('some_text:\*', index='test_index')
No hits

如何让第一个项目出现在搜索中?尽管各种搜索方法之间存在不一致,但他们似乎都同意我不允许返回“*”,但为什么呢?此外,转义 * 似乎会使问题变得更糟,这有点不寻常。(我假设库中可能存在一些自动转义,但这并不能真正解释直接 ES 查询)。

编辑:我应该提到它肯定是索引的。

>>>es.get('test_index', 'test_item', 1)

{'_index': 'test_index', '_version': 1, '_id': '1', 'found': True, '_type': 'test_item', '_source': {'some_text': '*'}}

不过,它可能已被存储,据我所知,这对于弹性搜索来说是一件特别的事情?

Edit2: ElasticSearch 文档谈论转义一些

4

1 回答 1

0

Ended up solving this by changing the analyzer to a whitespace analyzer. (It was a lucene issue, not elasticsearch, which was why it was tough to find!)

于 2014-09-14T01:02:04.657 回答