Elsticsearch:包含查询

时间:2016-02-18 15:51:44

标签: ajax elasticsearch elasticsearch-2.0

我的映射中有一个包含字符串数组的列

col1
["asd","fgh","wer"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk","fsdfd"]
["asd","trth","fdf"]

索引中未分析列col,我不想更改映射。

"col1":
{
    "type":"string",
    "index":"not_analyzed"
}

现在,我想检索字符串asd出现的所有记录。所以在这种情况下,我想要第一和第四条记录。我尝试使用查询

        query: {

                    wildcard:{
                        "col1":"asd"
                    }
                }

POST localhost:9200/indexName/test/_search

但这给了我空洞的结果?在这种情况下我应该使用哪个查询?

修改

所以我能够解决上述问题。这是一个跟进。请考虑这是我的数据

col1
["asd fd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

现在,数组包含一些包含多个单词的字符串。现在,我仍然想要返回第一和第四条记录。如果我使用我发布的解决方案,我只会得到第四个。如何在contains

中将col1逻辑应用于数组的每个元素

注意

部分解决方案是

{ "query": { "match_phrase_prefix": { "col1": "asd" } } }

再次,对于数据

col1
["asd fd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

它返回第一个和第四个记录。但是,如果我有

col1
["fd asd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

然后,它再次只返回第四个,这是可以理解的,asd不再是第一个记录中该值的前缀。

有没有办法进行contains类型匹配而不只是前缀匹配?

2 个答案:

答案 0 :(得分:2)

您可以使用简单的term查询,它应该可以正常工作

POST localhost:9200/indexName/test/_search
{
    "query": {
        "terms": { "col1" : "asd" }
    }
}

答案 1 :(得分:0)

所以,这是正确的查询

{

                    fields : ["col1","col2"],
                    query: {
                        filtered: {

                          query: {
                            match_all: {}
                          },
                          filter: {
                            terms: {
                              col1: ["asd"]
                            }
                          }
                        }
                      }

                }

最终答案

    query: {

                wildcard:{

                  col1:{

                          value:"*asd*"
                       }
                }
            }

:)