weixin_33711641 2016-02-18 15:51 采纳率: 0%
浏览 108

Elasticsearch:包含查询

I have a column in my mapping that holds an array of strings

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

The column col is not analyzed in the index and i do not want to change the mapping.

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

Now, i want to retrieve all records where the string asd appears. so in this case, i want the first and fourth records. I tried using the query

        query: {

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

with

POST localhost:9200/indexName/test/_search

but that gives me empty results? Which query should i use in this case?

Edit

So i was able to solve the above problem. Here is a follow up. Consider that this was my data

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

so now, the array contains some strings that have multiple words. Now, i still want to return the first and fourth record. If i go with the solution that i posted, i only get the fourth one. How can i apply the contains logic to each element of the array in col1?

Note

A partial solution is

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

so again, for the data

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

it returns the first and fourth records. However, if i have

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

then, once again it only returns the fourth one, which is understandable as now, asd is no longer a prefix for that value in the first record.

Is there a way to to a contains type match instead of just prefix match?

  • 写回答

2条回答 默认 最新

  • weixin_33714884 2016-02-18 15:53
    关注

    You can use a simple term query and it should work

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

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码