【问题标题】:Elasticsearch query_string - exact phrase issueElasticsearch query_string - 确切的短语问题
【发布时间】:2017-08-20 12:45:32
【问题描述】:

假设我有一个索引,并且我添加了一些文档,使用以下语句:

POST test/item/_bulk
{"id": 1, "text": "one two"}
{"id": 2, "text": "one two three"}
{"id": 3, "text": "three one two"}
{"id": 4, "text": "three one two four"}
{"id": 5, "text": "one two|"}
{"id": 6, "text": "|one two"}
{"id": 7, "text": "|one two|"}
{"id": 8, "text": "one|two"}
{"id": 9, "text": "one| two"}
{"id": 10, "text": "one |two"}
{"id": 11, "text": "one | two"}

我想要这个搜索:

GET test/item/_search
{
    "query": 
    {
        "query_string": 
        {
            "query": "\"one two\"",
            "fields": ["text"],
            "analyze_wildcard": "true",
            "allow_leading_wildcard": "true",
            "default_operator": "AND"
        }
    }
}

返回文档1-7

我在文档和查询中尝试了各种分析器和标记器(std、空格等),但它们都没有给我想要的结果。

例如,std 分析器返回所有文档,而空白分析器只返回 1-4。

是否有可以返回所需结果的分析器/标记器/参数?

注意:为了清楚起见,我的数据由没有共同特征的短字符串和 非常 长字符串组成。我给出的单词(一、二、三、四)和符号(|)只是为了方便起见,可以替换为任何其他单词和非单词字符。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    【讨论】:

    • 范围查询与我的需求无关,我的数据由没有共同特征的字符串组成。 “一”/“二”/“三”/“四”只是一个例子。我添加了一个注释来澄清这一点。不过还是谢谢!
    【解决方案2】:

    我认为您应该尝试模式分析器来分析您的数据。它允许您指定正则表达式来定义分割文本的模式。

    您应该创建一个自定义分析器并定义一个模式来标记您的数据。

    https://www.elastic.co/guide/en/elasticsearch/guide/master/custom-analyzers.html

    【讨论】:

    • 在很长的字符串上使用模式标记器时是否存在性能问题的危险?
    • 我无法确认,但也许有。但我认为最好在 elasticsearch 上完成,而不是通过编程方式完成。
    【解决方案3】:

    对不起,我昨天不明白你的意思。 存在一种解决方案,但它是否优化,我不确定: 首先你应该创建动态模板,并为你的字段设置 not_analyzed 模式:

    curl -XPOST 'localhost:9200/_template/template1' -d '
    {
        "template":"test_*",
        "mappings": {
            "item": {
                "dynamic_templates": [
                    {
                        "strings": {
                            "mapping": { 
                                "index": "not_analyzed",
                                "type": "string"
                            },
                            "match_mapping_type": "string"
                        }
                    }
                ]
            }
        },
        "aliases": {}
    }'
    

    然后我插入下一行:

    curl -XPOST localhost:9200/test_1/item/1 -d '{ "text": "one two"}'
    curl -XPOST localhost:9200/test_1/item/2 -d '{ "text": "one two three"}'
    curl -XPOST localhost:9200/test_1/item/3 -d '{ "text": "three one two"}'
    curl -XPOST localhost:9200/test_1/item/4 -d '{ "text": "three one two    four"}'
    curl -XPOST localhost:9200/test_1/item/5 -d '{ "text": "one two|"}'
    curl -XPOST localhost:9200/test_1/item/6 -d '{ "text": "|one two"}'
    curl -XPOST localhost:9200/test_1/item/7 -d '{ "text": "|one two|"}'
    curl -XPOST localhost:9200/test_1/item/8 -d '{ "text": "one|two"}'
    curl -XPOST localhost:9200/test_1/item/9 -d '{ "text": "one| two"}'
    curl -XPOST localhost:9200/test_1/item/10 -d '{ "text": "one |two"}'
    

    通过通配符查询,您可以返回必要的行:

    curl localhost:9200/test_1/_search -d '
    {
    "query": {
        "match" : {
            "test" : "one two"
        }
    }
    }'
    

    这个查询返回 7 行:

    nugusbayevkk@mediator:/data/databases/elasticsearch-5.2.2/bin$ curl localhost:9200/test_1/_search?filter_path=hits.total -d '
    {
    "query": {
        "wildcard" : {
            "text" : "*one two*"
        }
    }
    }'
    {"hits":{"total":7}}
    

    ?filter_path - 让我们显示一些字段,在这种情况下,它显示存在的总行数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多