Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 带有标点符号的Elasticsearch查询数据将导致错误的查询结果_Python 2.7_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Python 2.7,elasticsearch" /> elasticsearch,Python 2.7,elasticsearch" />

Python 2.7 带有标点符号的Elasticsearch查询数据将导致错误的查询结果

Python 2.7 带有标点符号的Elasticsearch查询数据将导致错误的查询结果,python-2.7,elasticsearch,Python 2.7,elasticsearch,案例介绍 我的案例是在elasticsearch索引中存储一些单词,每个单词都有自己的ID。 我的查询数据是一些消息。当查询数据消息中有一些标点符号时,Elasticsearch将返回错误的答案 示例: 例如,我在索引中存储了关键字“香蕉、苹果、钢笔”。我使用bulk_索引API存储它 查询数据1:“这是香蕉吗?” 正确的结果应该是hits关键字“banana”,但现在它什么也没有命中 查询数据2:“>>这是一本书” 结果应该是不命中任何内容,但现在它命中了索引中的所有关键字 如果没有标点符号,

案例介绍

我的案例是在elasticsearch索引中存储一些单词,每个单词都有自己的ID。 我的查询数据是一些消息。当查询数据消息中有一些标点符号时,Elasticsearch将返回错误的答案

示例:

例如,我在索引中存储了关键字“香蕉、苹果、钢笔”。我使用bulk_索引API存储它

查询数据1:“这是香蕉吗?”

正确的结果应该是hits关键字“banana”,但现在它什么也没有命中

查询数据2:“>>这是一本书”

结果应该是不命中任何内容,但现在它命中了索引中的所有关键字

如果没有标点符号,查询结果将正常工作

代码:

我的storeToIndex代码:(python,作为客户端的pyelasticsearch)

queryIndex()的我的代码

问题:

我可以使用regular express来解决它,但是有没有使用elasticsearch设置的解决方案?过滤器或API等?

环境配置:

Ubuntu 12.04桌面64位

Ubuntu中的Elasticsearch服务器,版本0.90.7,单节点

客户:pyelasticsearch

编程语言:python


使用的API:批量索引API、搜索API<代码>香蕉?被解释为以
香蕉
开头,以单个未指定字符结尾的术语。例如,这将匹配banana1。并且,
>..
正在创建一个开放式范围查询,这就是它匹配索引中所有内容的原因

我建议您考虑使用不同的查询类型,例如,针对此类情况设计的

查看四个查询(请参见左下面板中的搜索选项卡),为方便起见,将其导出为Curl命令:

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "settings": {}
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"somefield":"banana"}
{"index":{"_index":"play","_type":"type"}}
{"somefield":"apple"}
{"index":{"_index":"play","_type":"type"}}
{"somefield":"pen"}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "query_string": {
            "query": "is this banana?"
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "somefield": {
                "query": "is this banana?"
            }
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "query_string": {
            "query": ">> it is a book"
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "somefield": {
                "query": ">> it is a book"
            }
        }
    }
}
'

欢迎来到堆栈溢出!看起来你想让我们为你写些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是,包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、堆栈跟踪、编译器错误——任何适用的)。你提供的细节越多,你可能得到的答案就越多。检查,我知道如何使用python来解决这个问题,但在我的例子中,我必须在elasticsearch中解决它。你知道我可以使用哪种API、过滤器或elese吗?我现在使用的API是批量索引API,搜索API。
query={"query":{"query_string":{"query":"%s"%query_data}}}
 es=ElasticSearch('http://localhost:9200/')
 search_result=es.search(query=query,index=index_name,doc_type='json')
#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "settings": {}
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"somefield":"banana"}
{"index":{"_index":"play","_type":"type"}}
{"somefield":"apple"}
{"index":{"_index":"play","_type":"type"}}
{"somefield":"pen"}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "query_string": {
            "query": "is this banana?"
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "somefield": {
                "query": "is this banana?"
            }
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "query_string": {
            "query": ">> it is a book"
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "somefield": {
                "query": ">> it is a book"
            }
        }
    }
}
'