python-3.x - 无法在 Elasticsearch 中搜索带有符号的查询

标签 python-3.x elasticsearch indexing lucene

我一直在尝试使用 elasticsearch python 客户端 来匹配查询,但即使在使用转义字符并设置了一些自定义分析器和映射之后我也无法匹配它他们。我想使用 & 进行搜索,但它没有给出任何响应。

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])


doc1 = {
    'name': 'numb',
    'band': 'linkin_park',
    'year': '2006'
}

doc2 = {
    'name': 'Powerless &',
    'band': 'linkin_park',
    'year': '2006'
}
doc3 = {
    'name': 'Crawling !',
    'band': 'linkin_park',
    'year': '2006'
    }

doc =[doc1, doc2, doc3]
'''
create_index = {
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "type": "custom",
                    "filter": [
                        "lowercase"
                    ],
                    "tokenizer": "whitespace"
                }
            }
        }
    }
}

es.indices.create(index="idx_temp", body=create_index)
'''
for i in range(3):
    es.index(index="idx_temp", doc_type='_doc', id=i, body=doc[i])


my_mapping = {
  "properties": {
      "name": {
          "type": "text",
          "fields": {
              "keyword": {
                  "type": "keyword",
                  'ignore_above': 256
              }
          },
          "analyzer": "my_analyzer"
          "search_analyzer": "my_analyzer"
      },
      "band": {
          "type": "text",
          "fields": {
              "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
              }
          },
          "analyzer": "my_analyzer"
          "search_analyzer": "my_analyzer"
      },
      "year": {
          "type": "text",
          "fields": {
              "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
              }
          },
          "analyzer": "my_analyzer"
          "search_analyzer": "my_analyzer"
      }
  }
}

es.indices.put_mapping(index='idx_temp', body=my_mapping, doc_type='_doc', include_type_name=True)

res = es.search(index='idx_temp', body={
    "query": {
        "match": {
            "name": {
                "query": "powerless &",
                "fuzziness": 3

            }
        }
    }
})

for hit in res['hits']['hits']:
    print(hit['_source'])

预期的输出是 'name': 'Poweeerless &', 但我得到 0 次点击并且没有返回任何值。

最佳答案

所以我通过添加另一个字段解决了这个问题

 "search_quote_analyzer": "my_analyzer"

到设置字段之后

"analyzer": "my_analyzer"
"search_analyzer": "my_analyzer"

然后我通过在查询中使用 & 进行搜索来获取输出

'name': 'Poweeerless &'

关于python-3.x - 无法在 Elasticsearch 中搜索带有符号的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885441/

相关文章:

regex - Elasticsearch中的时间戳正则表达式

elasticsearch - 如何计算Elasticsearch中父字段的总和?

python - PyCharm 无法识别我的 python 解释器和 PIP 包。也许路径问题?

python - 根据范围内的数字查找字符串

python - Python 中 tkinter 中的协议(protocol)

elasticsearch - 多重匹配并将查询一起匹配

python - Cython:(为什么/何时)最好使用 Py_ssize_t 进行索引?

javascript - 也可以在另一个函数中使用在 select 标记中声明的 onchange 函数元素吗?

python - python 2D列表的索引

python - 如何将python3命令更改为python