elasticsearch - 如何在elasticsearch simple_query_string中包含除空格以外的所有字符?

标签 elasticsearch

我正在寻找elasticsearch中最简单的查询系统,其中唯一的分隔符是空格。我想查询几乎所有类型的字符都附带的文本中的用户名,例如@用户名或@ u $ er-na_me

它以为这会很容易,但是经过长时间的搜索,我才找到了一个“空白”分析器:
query.json:

{
  "query": {
    "simple_query_string" : {
        "query": "@username",
        "fields": ["mytextfield"],
        "analyzer": "whitespace",
        "default_operator": "and"
    }
  }
}

我运行的是:curl -X POST "http://localhost:9200/my.index/_search?pretty" -H 'Content-Type: application/json' -d '@query.json'
但是,它什么也不返回。

其他详情:
  • "query": "username"仍然有效
  • 如果删除"analyzer": "whitespace",则"query": "username""query": "@username"的结果相同
  • 我在SO中看到过类似的帖子(例如Simple query string with special characters such as ( and =),它们似乎在其中创建新的索引或映射。如果这是要走的路,我将不胜感激一些资源来理解工作流程。

  • 因此,总而言之,是否有任何简单的方法来配置elasticsearch或查询以仅使用空格(最好是基本标点符号)进行标记化?

    最佳答案

    toto_tico,您需要分析您的字段并在映射中进行指定,如下所示:

    1)使用单个分析字段创建索引:

    PUT totoindex
    {
      "mappings": {
          "_doc": {
            "properties": {
              "mytextfield":{"type":"text", "analyzer": "whitespace"}
            }
          }
        }
    }
    

    2)索引一些样本文件:
    POST totoindex/_doc/
    {
      "mytextfield": "@toto_tico"
    }
    

    3)搜索文件:
    POST totoindex/_doc/_search
    {
      "query": {
        "simple_query_string" : {
            "query": "@toto_tico",
            "fields": ["mytextfield"]
        }
      }
    }
    

    回应:
    {
      "took": 12,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
          {
            "_index": "totoindex",
            "_type": "_doc",
            "_id": "MtorKW8BavUEUOqEr6k_",
            "_score": 0.2876821,
            "_source": {
              "mytextfield": "@toto_tico"
            }
          }
        ]
      }
    }
    

    关于elasticsearch - 如何在elasticsearch simple_query_string中包含除空格以外的所有字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59435314/

    相关文章:

    java - Elasticsearch存储库分页未返回正确的页面大小并且未对数据进行排序

    Elastic 5.x 的 Azure 模板

    docker-compose ERROR : bootstrap checks failed | max > virtual memory areas vm. max_map_count [65530] 太低,增加到 > 至少 [262144]

    elasticsearch - 将搜索结果缩小到多个类别

    date - Kibana 4不是可视化菜单中的好日子

    spring-boot - ElasticsearchRestTemplate滚动获取下一页

    sql - SQL 和 elasticsearch 之间的良好实践

    elasticsearch - 意外发生错误 'Fielddata is disabled on text fields by default'

    curl - Elasticsearch must_not 查询与正则表达式不匹配

    Elasticsearch 如何使用命令行索引文本文件