@Test
  public void testSimpleMapper() throws Exception {
    DocumentMapperParser mapperParser = MapperTests.newParser();
    DocumentMapper docMapper =
        doc(
                "test",
                rootObject("person")
                    .add(object("name").add(stringField("first").store(YES).index(Field.Index.NO))))
            .build(mapperParser);

    BytesReference json =
        new BytesArray(
            copyToBytesFromClasspath(
                "/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
    Document doc = docMapper.parse("person", "1", json).rootDoc();

    assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
    assertThat(
        doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
    assertThat(
        docMapper.mappers().name("first").mapper().names().fullName(), equalTo("name.first"));
    //        System.out.println("Document: " + doc);
    //        System.out.println("Json: " + docMapper.sourceMapper().value(doc));
    doc = docMapper.parse(json).rootDoc();
    //        System.out.println("Document: " + doc);
    //        System.out.println("Json: " + docMapper.sourceMapper().value(doc));
  }
Exemple #2
0
 public void search01() {
   try {
     IndexReader reader = IndexReader.open(directory);
     IndexSearcher searcher = new IndexSearcher(reader);
     TermQuery query = new TermQuery(new Term("email", "*****@*****.**"));
     TopDocs tds = searcher.search(query, 10);
     for (ScoreDoc sd : tds.scoreDocs) {
       Document doc = searcher.doc(sd.doc);
       System.out.println(
           "("
               + sd.doc
               + "-"
               + doc.getBoost()
               + "-"
               + sd.score
               + ")"
               + doc.get("name")
               + "["
               + doc.get("email")
               + "]-->"
               + doc.get("id")
               + ","
               + doc.get("attach")
               + ","
               + doc.get("date")
               + ","
               + doc.getValues("email")[1]);
     }
     reader.close();
   } catch (CorruptIndexException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 @Test
 public void testSimpleParserNoTypeNoId() throws Exception {
   String mapping =
       copyToStringFromClasspath(
           "/org/elasticsearch/test/unit/index/mapper/simple/test-mapping.json");
   DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
   BytesReference json =
       new BytesArray(
           copyToBytesFromClasspath(
               "/org/elasticsearch/test/unit/index/mapper/simple/test1-notype-noid.json"));
   Document doc = docMapper.parse("person", "1", json).rootDoc();
   assertThat(
       doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
   assertThat((double) doc.getBoost(), closeTo(3.7, 0.01));
   assertThat(
       doc.get(docMapper.mappers().name("first").mapper().names().indexName()), equalTo("shay"));
   //        System.out.println("Document: " + doc);
   //        System.out.println("Json: " + docMapper.sourceMapper().value(doc));
 }