1 package org.wikimedia.search.extra.fuzzylike;
2
3 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
4 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
5 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
6 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
7 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoSearchHits;
8 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
9 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
10
11 import java.io.IOException;
12 import java.util.concurrent.ExecutionException;
13
14 import org.elasticsearch.action.index.IndexRequestBuilder;
15 import org.elasticsearch.action.search.SearchResponse;
16 import org.elasticsearch.common.unit.Fuzziness;
17 import org.elasticsearch.common.xcontent.XContentBuilder;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.wikimedia.search.extra.AbstractPluginIntegrationTest;
21
22 @Deprecated
23 public class FuzzyLikeThisIntegrationTest extends AbstractPluginIntegrationTest {
24
25 @Before
26 private void setup() throws IOException, InterruptedException, ExecutionException {
27 XContentBuilder mapping = jsonBuilder().startObject();
28 mapping.startObject("test").startObject("properties");
29 mapping.startObject("test");
30 mapping.field("type", "text");
31 mapping.endObject()
32 .endObject()
33 .endObject()
34 .endObject();
35
36 assertAcked(prepareCreate("test").addMapping("test", mapping));
37 ensureGreen();
38
39 indexRandom(false, doc("image", "There is nothing worse than a sharp image of a fuzzy concept."));
40 indexRandom(false, doc("humans", "From time to time, it is worth wandering around the fuzzy border " +
41 "regions of what you do, if only to remind yourself that no human activity is an island."));
42 indexRandom(false, doc("engineers", "Unfortunately, I'm an engineer. " +
43 "I'm always thinking about, what's the task and how do I get it done? " +
44 "And some of my tasks are pretty broad, and pretty fuzzy, and pretty funky, but that's the way I think."));
45 indexRandom(false, doc("science", "People assume that science is a very cold sort of profession, " +
46 "whereas writing novels is a warm and fuzzy intuitive thing. But in fact, they are not at all different."));
47 indexRandom(false, doc("nostalgia", "Nostalgia is something we think of as fuzzy. " +
48 "But it's pain. Pain concerning the past."));
49 refresh();
50 }
51
52 private IndexRequestBuilder doc(String id, String fieldValue) {
53 return client().prepareIndex("test", "test", id).setSource("test", fieldValue);
54 }
55
56 @Test
57 public void testFuzzyLikeThis() {
58 FuzzyLikeThisQueryBuilder builder;
59 SearchResponse resp;
60
61 builder = fuzzyLikeThisQuery("test", "sharp image fuzzy concpt");
62 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
63 assertHitCount(resp, 5);
64 assertFirstHit(resp, hasId("image"));
65
66 builder = fuzzyLikeThisQuery("test", "sharp image concpt");
67 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
68 assertHitCount(resp, 1);
69 assertFirstHit(resp, hasId("image"));
70
71 builder = fuzzyLikeThisQuery("test", "nostalagia").fuzziness(Fuzziness.ZERO);
72 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
73 assertNoSearchHits(resp);
74
75 builder = fuzzyLikeThisQuery("test", "nostalagio").fuzziness(Fuzziness.ONE);
76 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
77 assertNoSearchHits(resp);
78
79
80 builder = fuzzyLikeThisQuery("test", "nostalagio").fuzziness(Fuzziness.AUTO);
81 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
82 assertNoSearchHits(resp);
83
84 builder = fuzzyLikeThisQuery("test", "nostalagio").fuzziness(Fuzziness.TWO);
85 resp = client().prepareSearch("test").setTypes("test").setQuery(builder).get();
86 assertSearchHits(resp, "nostalgia");
87 }
88
89 @Deprecated
90 public FuzzyLikeThisQueryBuilder fuzzyLikeThisQuery(String field, String likeText) {
91 return new FuzzyLikeThisQueryBuilder(new String[]{field}, likeText);
92 }
93 }