|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.query; |
| 21 | + |
| 22 | +import org.elasticsearch.action.search.SearchResponse; |
| 23 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
| 24 | +import org.elasticsearch.index.query.SimpleQueryStringBuilder; |
| 25 | +import org.elasticsearch.index.query.SimpleQueryStringFlag; |
| 26 | +import org.elasticsearch.test.ElasticsearchIntegrationTest; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Locale; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | + |
| 33 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 34 | +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; |
| 35 | +import static org.elasticsearch.index.query.QueryBuilders.queryString; |
| 36 | +import static org.elasticsearch.index.query.QueryBuilders.simpleQueryString; |
| 37 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; |
| 38 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; |
| 39 | +import static org.hamcrest.Matchers.equalTo; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for the {@code simple_query_string} query |
| 43 | + */ |
| 44 | +public class SimpleQueryStringTests extends ElasticsearchIntegrationTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testSimpleQueryString() throws ExecutionException, InterruptedException { |
| 48 | + createIndex("test"); |
| 49 | + indexRandom(true, false, |
| 50 | + client().prepareIndex("test", "type1", "1").setSource("body", "foo"), |
| 51 | + client().prepareIndex("test", "type1", "2").setSource("body", "bar"), |
| 52 | + client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), |
| 53 | + client().prepareIndex("test", "type1", "4").setSource("body", "quux baz eggplant"), |
| 54 | + client().prepareIndex("test", "type1", "5").setSource("body", "quux baz spaghetti"), |
| 55 | + client().prepareIndex("test", "type1", "6").setSource("otherbody", "spaghetti")); |
| 56 | + |
| 57 | + SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryString("foo bar")).get(); |
| 58 | + assertHitCount(searchResponse, 3l); |
| 59 | + assertSearchHits(searchResponse, "1", "2", "3"); |
| 60 | + |
| 61 | + searchResponse = client().prepareSearch().setQuery( |
| 62 | + simpleQueryString("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get(); |
| 63 | + assertHitCount(searchResponse, 1l); |
| 64 | + assertFirstHit(searchResponse, hasId("3")); |
| 65 | + |
| 66 | + searchResponse = client().prepareSearch().setQuery(simpleQueryString("\"quux baz\" +(eggplant | spaghetti)")).get(); |
| 67 | + assertHitCount(searchResponse, 2l); |
| 68 | + assertSearchHits(searchResponse, "4", "5"); |
| 69 | + |
| 70 | + searchResponse = client().prepareSearch().setQuery( |
| 71 | + simpleQueryString("eggplants").analyzer("snowball")).get(); |
| 72 | + assertHitCount(searchResponse, 1l); |
| 73 | + assertFirstHit(searchResponse, hasId("4")); |
| 74 | + |
| 75 | + searchResponse = client().prepareSearch().setQuery( |
| 76 | + simpleQueryString("spaghetti").field("body", 10.0f).field("otherbody", 2.0f).queryName("myquery")).get(); |
| 77 | + assertHitCount(searchResponse, 2l); |
| 78 | + assertFirstHit(searchResponse, hasId("5")); |
| 79 | + assertSearchHits(searchResponse, "5", "6"); |
| 80 | + assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("myquery")); |
| 81 | + |
| 82 | + searchResponse = client().prepareSearch().setQuery(simpleQueryString("spaghetti").field("*body")).get(); |
| 83 | + assertHitCount(searchResponse, 2l); |
| 84 | + assertSearchHits(searchResponse, "5", "6"); |
| 85 | + |
| 86 | + // Have to bypass the builder here because the builder always uses "fields" instead of "field" |
| 87 | + searchResponse = client().prepareSearch().setQuery("{\"simple_query_string\": {\"query\": \"spaghetti\", \"field\": \"_all\"}}").get(); |
| 88 | + assertHitCount(searchResponse, 2l); |
| 89 | + assertSearchHits(searchResponse, "5", "6"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testSimpleQueryStringLowercasing() { |
| 94 | + createIndex("test"); |
| 95 | + client().prepareIndex("test", "type1", "1").setSource("body", "Professional").get(); |
| 96 | + refresh(); |
| 97 | + |
| 98 | + SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryString("Professio*")).get(); |
| 99 | + assertHitCount(searchResponse, 1l); |
| 100 | + assertSearchHits(searchResponse, "1"); |
| 101 | + |
| 102 | + searchResponse = client().prepareSearch().setQuery( |
| 103 | + simpleQueryString("Professio*").lowercaseExpandedTerms(false)).get(); |
| 104 | + assertHitCount(searchResponse, 0l); |
| 105 | + |
| 106 | + searchResponse = client().prepareSearch().setQuery( |
| 107 | + simpleQueryString("Professionan~1")).get(); |
| 108 | + assertHitCount(searchResponse, 1l); |
| 109 | + assertSearchHits(searchResponse, "1"); |
| 110 | + |
| 111 | + searchResponse = client().prepareSearch().setQuery( |
| 112 | + simpleQueryString("Professionan~1").lowercaseExpandedTerms(false)).get(); |
| 113 | + assertHitCount(searchResponse, 0l); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testQueryStringLocale() { |
| 118 | + createIndex("test"); |
| 119 | + client().prepareIndex("test", "type1", "1").setSource("body", "bılly").get(); |
| 120 | + refresh(); |
| 121 | + |
| 122 | + SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryString("BILL*")).get(); |
| 123 | + assertHitCount(searchResponse, 0l); |
| 124 | + searchResponse = client().prepareSearch().setQuery(queryString("body:BILL*")).get(); |
| 125 | + assertHitCount(searchResponse, 0l); |
| 126 | + |
| 127 | + searchResponse = client().prepareSearch().setQuery( |
| 128 | + simpleQueryString("BILL*").locale(new Locale("tr", "TR"))).get(); |
| 129 | + assertHitCount(searchResponse, 1l); |
| 130 | + assertSearchHits(searchResponse, "1"); |
| 131 | + searchResponse = client().prepareSearch().setQuery( |
| 132 | + queryString("body:BILL*").locale(new Locale("tr", "TR"))).get(); |
| 133 | + assertHitCount(searchResponse, 1l); |
| 134 | + assertSearchHits(searchResponse, "1"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testNestedFieldSimpleQueryString() throws IOException { |
| 139 | + assertAcked(prepareCreate("test") |
| 140 | + .addMapping("type1", jsonBuilder() |
| 141 | + .startObject() |
| 142 | + .startObject("type1") |
| 143 | + .startObject("properties") |
| 144 | + .startObject("body").field("type", "string") |
| 145 | + .startObject("fields") |
| 146 | + .startObject("sub").field("type", "string") |
| 147 | + .endObject() // sub |
| 148 | + .endObject() // fields |
| 149 | + .endObject() // body |
| 150 | + .endObject() // properties |
| 151 | + .endObject() // type1 |
| 152 | + .endObject())); |
| 153 | + client().prepareIndex("test", "type1", "1").setSource("body", "foo bar baz").get(); |
| 154 | + refresh(); |
| 155 | + |
| 156 | + SearchResponse searchResponse = client().prepareSearch().setQuery( |
| 157 | + simpleQueryString("foo bar baz").field("body")).get(); |
| 158 | + assertHitCount(searchResponse, 1l); |
| 159 | + assertSearchHits(searchResponse, "1"); |
| 160 | + |
| 161 | + searchResponse = client().prepareSearch().setQuery( |
| 162 | + simpleQueryString("foo bar baz").field("type1.body")).get(); |
| 163 | + assertHitCount(searchResponse, 1l); |
| 164 | + assertSearchHits(searchResponse, "1"); |
| 165 | + |
| 166 | + searchResponse = client().prepareSearch().setQuery( |
| 167 | + simpleQueryString("foo bar baz").field("body.sub")).get(); |
| 168 | + assertHitCount(searchResponse, 1l); |
| 169 | + assertSearchHits(searchResponse, "1"); |
| 170 | + |
| 171 | + searchResponse = client().prepareSearch().setQuery( |
| 172 | + simpleQueryString("foo bar baz").field("type1.body.sub")).get(); |
| 173 | + assertHitCount(searchResponse, 1l); |
| 174 | + assertSearchHits(searchResponse, "1"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testSimpleQueryStringFlags() throws ExecutionException, InterruptedException { |
| 179 | + createIndex("test"); |
| 180 | + indexRandom(true, |
| 181 | + client().prepareIndex("test", "type1", "1").setSource("body", "foo"), |
| 182 | + client().prepareIndex("test", "type1", "2").setSource("body", "bar"), |
| 183 | + client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), |
| 184 | + client().prepareIndex("test", "type1", "4").setSource("body", "quux baz eggplant"), |
| 185 | + client().prepareIndex("test", "type1", "5").setSource("body", "quux baz spaghetti"), |
| 186 | + client().prepareIndex("test", "type1", "6").setSource("otherbody", "spaghetti")); |
| 187 | + |
| 188 | + SearchResponse searchResponse = client().prepareSearch().setQuery( |
| 189 | + simpleQueryString("foo bar").flags(SimpleQueryStringFlag.ALL)).get(); |
| 190 | + assertHitCount(searchResponse, 3l); |
| 191 | + assertSearchHits(searchResponse, "1", "2", "3"); |
| 192 | + |
| 193 | + // Sending a negative 'flags' value is the same as SimpleQueryStringFlag.ALL |
| 194 | + searchResponse = client().prepareSearch().setQuery("{\"simple_query_string\": {\"query\": \"foo bar\", \"flags\": -1}}").get(); |
| 195 | + assertHitCount(searchResponse, 3l); |
| 196 | + assertSearchHits(searchResponse, "1", "2", "3"); |
| 197 | + |
| 198 | + searchResponse = client().prepareSearch().setQuery( |
| 199 | + simpleQueryString("foo | bar") |
| 200 | + .defaultOperator(SimpleQueryStringBuilder.Operator.AND) |
| 201 | + .flags(SimpleQueryStringFlag.OR)).get(); |
| 202 | + assertHitCount(searchResponse, 3l); |
| 203 | + assertSearchHits(searchResponse, "1", "2", "3"); |
| 204 | + |
| 205 | + searchResponse = client().prepareSearch().setQuery( |
| 206 | + simpleQueryString("foo | bar") |
| 207 | + .defaultOperator(SimpleQueryStringBuilder.Operator.AND) |
| 208 | + .flags(SimpleQueryStringFlag.NONE)).get(); |
| 209 | + assertHitCount(searchResponse, 1l); |
| 210 | + assertFirstHit(searchResponse, hasId("3")); |
| 211 | + |
| 212 | + searchResponse = client().prepareSearch().setQuery( |
| 213 | + simpleQueryString("baz | egg*") |
| 214 | + .defaultOperator(SimpleQueryStringBuilder.Operator.AND) |
| 215 | + .flags(SimpleQueryStringFlag.NONE)).get(); |
| 216 | + assertHitCount(searchResponse, 0l); |
| 217 | + |
| 218 | + searchResponse = client().prepareSearch().setSource("{\n" + |
| 219 | + " \"query\": {\n" + |
| 220 | + " \"simple_query_string\": {\n" + |
| 221 | + " \"query\": \"foo|bar\",\n" + |
| 222 | + " \"default_operator\": \"AND\"," + |
| 223 | + " \"flags\": \"NONE\"\n" + |
| 224 | + " }\n" + |
| 225 | + " }\n" + |
| 226 | + "}").get(); |
| 227 | + assertHitCount(searchResponse, 1l); |
| 228 | + |
| 229 | + searchResponse = client().prepareSearch().setQuery( |
| 230 | + simpleQueryString("baz | egg*") |
| 231 | + .defaultOperator(SimpleQueryStringBuilder.Operator.AND) |
| 232 | + .flags(SimpleQueryStringFlag.WHITESPACE, SimpleQueryStringFlag.PREFIX)).get(); |
| 233 | + assertHitCount(searchResponse, 1l); |
| 234 | + assertFirstHit(searchResponse, hasId("4")); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + public void testSimpleQueryStringLenient() throws ExecutionException, InterruptedException { |
| 239 | + createIndex("test1", "test2"); |
| 240 | + indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("field", "foo"), |
| 241 | + client().prepareIndex("test2", "type1", "10").setSource("field", 5)); |
| 242 | + refresh(); |
| 243 | + |
| 244 | + SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryString("foo").field("field")).get(); |
| 245 | + assertFailures(searchResponse); |
| 246 | + assertHitCount(searchResponse, 1l); |
| 247 | + assertSearchHits(searchResponse, "1"); |
| 248 | + |
| 249 | + searchResponse = client().prepareSearch().setQuery(simpleQueryString("foo").field("field").lenient(true)).get(); |
| 250 | + assertNoFailures(searchResponse); |
| 251 | + assertHitCount(searchResponse, 1l); |
| 252 | + assertSearchHits(searchResponse, "1"); |
| 253 | + } |
| 254 | + |
| 255 | + @Test // see: https://github.com/elasticsearch/elasticsearch/issues/7967 |
| 256 | + public void testLenientFlagBeingTooLenient() throws Exception { |
| 257 | + indexRandom(true, |
| 258 | + client().prepareIndex("test", "doc", "1").setSource("num", 1, "body", "foo bar baz"), |
| 259 | + client().prepareIndex("test", "doc", "2").setSource("num", 2, "body", "eggplant spaghetti lasagna")); |
| 260 | + |
| 261 | + BoolQueryBuilder q = boolQuery().should(simpleQueryString("bar").field("num").field("body").lenient(true)); |
| 262 | + SearchResponse resp = client().prepareSearch("test").setQuery(q).get(); |
| 263 | + assertNoFailures(resp); |
| 264 | + // the bug is that this would be parsed into basically a match_all |
| 265 | + // query and this would match both documents |
| 266 | + assertHitCount(resp, 1); |
| 267 | + assertSearchHits(resp, "1"); |
| 268 | + } |
| 269 | +} |
0 commit comments